How to select multiple picture from galery using GMImagePicker in xamarin IOS?

SreeeeSreeee INMember ✭✭✭✭✭
edited January 14 in Xamarin.Forms

Hi, 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?

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    I created a sample for iOS part.

  • SreeeeSreeee INMember ✭✭✭✭✭

    @ColeX said:
    I created a sample for iOS part.

    Thanks, I will go through this and update you...

  • SreeeeSreeee INMember ✭✭✭✭✭

    hi, @ColeX One question :)

    string s = Save(UIImage.LoadFromData(data), strs[strs.Length - 1]);          
        MessagingCenter.Send<App, string>((App)Xamarin.Forms.Application.Current, "ImagesSelected", s);
    

    Sending only one picture path by MessagingCenter. Why you avoid List as we use in the android part?

  • SreeeeSreeee INMember ✭✭✭✭✭
    public void SelectedImage()
        {
            var picker = new GMImagePickerController();
            List<string> images = new List<string>();
            picker.FinishedPickingAssets += (sender, args) => {
                foreach (var item in args.Assets)
                {
                    PHImageManager.DefaultManager.RequestImageData(item, null, (NSData data, NSString dataUti, UIImageOrientation orientation, NSDictionary info) => {
                        NSUrl url = info.ObjectForKey(new NSString("PHImageFileURLKey")) as NSUrl;
                        string urlString = url.AbsoluteString;
    
                        string[] strs = urlString.Split("/");
    
                        string filepath = Save(UIImage.LoadFromData(data), strs[strs.Length - 1]);
                        images.Add(filepath);
                    });
                }
                MessagingCenter.Send<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelected", images);
            };
            UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(picker, true, null);
        }
    

    Is this work?

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai
    edited January 15

    @Sreeee Because the method PHImageManager.DefaultManager.RequestImageData is executed asynchronously. So MessagingCenter.Send will be called earlier than RequestImageData . So ,when you call the MessagingCenter.Send ,the list still null.

  • SreeeeSreeee INMember ✭✭✭✭✭
    edited January 15

    Hi @ColeX and @Quintán

    When I build the project in Mac getting an error:

    Any casting needed?

  • SreeeeSreeee INMember ✭✭✭✭✭
    edited January 15

    @Sreeee said:
    Hi @ColeX and @Quintán

    When I build the project in Mac getting an error:

    Any casting needed?

    Update: Removed double quotes and add single quotes solved that problem.

    string[] strs = urlString.Split('/');
    
  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    @Sreeee I have downloaded your project and it works fine.

  • SreeeeSreeee INMember ✭✭✭✭✭

    @Quintán said:
    @Sreeee I have downloaded your project and it works fine.

    Did you make any modifications?

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    No , just cleaned and rebuilded the project.

Sign In or Register to comment.