1

I am following this blog for selecting multiple pictures from the gallery. For IOS I am Using GMImagePicker for selecting multiple pictures from the gallery.(In the blog suggesting elcimagepicker, but that is not available in Nuget Store now)

I go through the GMImagePicker usage part but didn't find how to add the selected images to List and pass that value in MessagingCenter(like the android implementation). In that usage part only telling about the picker settings. Anyone please give me any sample code for doing this feature?

Hi Lucas Zhang - MSFT, I tried your code but one question. Here you are passing only one file path through the messagecenter, so should I use a List for sending multiple file paths?

I am passing the picture paths as a string List from android. Please have a look at the android part code added below. Should I do like this in IOS?

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);

        if (resultCode == Result.Ok)
        {
            List<string> images = new List<string>();

            if (data != null)
            {
                ClipData clipData = data.ClipData;
                if (clipData != null)
                {
                    for (int i = 0; i < clipData.ItemCount; i++)
                    {
                        ClipData.Item item = clipData.GetItemAt(i);
                        Android.Net.Uri uri = item.Uri;
                        var path = GetRealPathFromURI(uri);

                        if (path != null)
                        {
                            //Rotate Image
                            var imageRotated = ImageHelpers.RotateImage(path);
                            var newPath = ImageHelpers.SaveFile("TmpPictures", imageRotated, System.DateTime.Now.ToString("yyyyMMddHHmmssfff"));
                            images.Add(newPath);
                        }
                    }
                }
                else
                {
                    Android.Net.Uri uri = data.Data;
                    var path = GetRealPathFromURI(uri);

                    if (path != null)
                    {
                        //Rotate Image
                        var imageRotated = ImageHelpers.RotateImage(path);
                        var newPath = ImageHelpers.SaveFile("TmpPictures", imageRotated, System.DateTime.Now.ToString("yyyyMMddHHmmssfff"));
                        images.Add(newPath);
                    }
                }

                MessagingCenter.Send<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelected", images);
            }
        }
    }

Also, I am getting an error, screenshot adding below:

enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sreejith Sree
  • 3,055
  • 4
  • 36
  • 105

1 Answers1

0

GMImagePicker will return a list contains PHAsset .So you could firstly get the filePath of the images and then pass them to forms by using MessagingCenter and DependencyService.Refer the following code.

in Forms, create an interface

using System;

namespace app1
{
  public interface ISelectMultiImage
  {
    void SelectedImage();
  }
}

in iOS project

using System;
using Xamarin.Forms;
using UIKit;
using GMImagePicker;
using Photos;
using Foundation;

[assembly:Dependency(typeof(SelectMultiImageImplementation))]
namespace xxx.iOS
{
  public class SelectMultiImageImplementation:ISelectMultiImage
  {
    public SelectMultiImageImplementation()
    {
    }


    string Save(UIImage image, string name)
    {
        var documentsDirectory = Environment.GetFolderPath
                              (Environment.SpecialFolder.Personal);
        string jpgFilename = System.IO.Path.Combine(documentsDirectory, name); // hardcoded filename, overwritten each time
        NSData imgData = image.AsJPEG();
        if (imgData.Save(jpgFilename, false, out NSError err))
        {
            return jpgFilename;
        }
        else
        {
            Console.WriteLine("NOT saved as " + jpgFilename + " because" + err.LocalizedDescription);
            return null;
        }
    }

    public void SelectedImage()
    {

        var picker = new GMImagePickerController();
        picker.FinishedPickingAssets += (s, args) => {


            PHAsset[] assets = args.Assets;



            foreach (PHAsset asset in assets)
            {
                PHImageManager.DefaultManager.RequestImageData(asset, null, (NSData data, NSString dataUti, UIImageOrientation orientation, NSDictionary info) =>
                {
                    NSUrl url = NSUrl.FromString(info.ValueForKey(new NSString("PHImageFileURLKey")).ToString());

                    string[] strs = url.Split("/");


                    UIImage image = UIImage.LoadFromData(data);

                    string file = Save(UIImage.LoadFromData(data), strs[strs.Length - 1]);


                    MessagingCenter.Send<Object, string>(this, "ImagesSelected", file);

                }
                );

            }

        };
       UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(picker, true,null);

    }
  }
}

in your contentPages

...
List<string> selectedImages;
...

public MyPage()
{
   selectedImages = new List<string>();
   InitializeComponent();

   MessagingCenter.Subscribe<Object,string>(this, "ImagesSelected",(object arg1,string arg2) =>
        {
            string source = arg2;

            selectedImages.Add(source);

        });

 } 

If you want to select the images ,call the method

 DependencyService.Get<ISelectMultiImage>().SelectedImage();
Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22