Save ImageStream to gallery in xamarin.iOS

kalkotekedarkalkotekedar USMember ✭✭
edited November 2017 in Xamarin.iOS

I want to save my ImageStream to the gallery in xamarin.iOS, now I am doing the code as shown below

 public string SavePictureToDisk(string filename, MemoryStream imageStream)
 {
        var documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        string imageFilename = System.IO.Path.Combine(documentsDirectory + "/AppPhoto", filename);
        var imageData = new UIImage(NSData.FromArray(imageStream.ToArray()));
        NSData pngImg = imageData.AsJPEG();
        if (pngImg.Save(imageFilename, false, out NSError err))
      {
              return imageFilename;
      }
        else
      {
              Console.WriteLine("NOT saved as " + imageFilename + " because" + err.LocalizedDescription);
              return null;
      }
 }

Answers

  • TedRogersTedRogers USMember ✭✭✭✭

    @kalkotekedar Kind of hard to help since you don't describe the problem you are seeing. You will have to add code to make sure the "AppPhoto" directory has been created but otherwise you would have to describe the problem you are seeing in detail.

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai
    edited November 2017

    @kalkotekedar

    So I think you are asking how to save the image in the Photos app. If so, this should help:
    https://developer.xamarin.com/recipes/ios/media/video_and_photos/save_photo_to_album_with_metadata/

    but as of iOS 10 you need to ask permission from the user to access there Photo library. See:
    https://blog.xamarin.com/new-ios-10-privacy-permission-settings/

    So in order to have a complete answer here that does not rely on links:

    Open your Info.plist file and select the Source tab. [a]

    Click the small green + button at the end of the list of Properties [b]. Do this twice, once to add the Camera Usage Description and once to add the Photo Library Usage Description (already added in the screenshot)

    Select the Privacy options as noted in [c] and [d] and enter a string for each that is what will be displayed to the user in the alert requesting permission from the user.

    Add the following code to request permission from the user, best placed in startup code somewhere, i.e. AppDelegate.FinishedLaunching:

    PHPhotoLibrary.RequestAuthorization(status =>
    {
          switch (status)
        {
               case PHAuthorizationStatus.Authorized:
                // Add code do run if user authorized permission, if needed.
                    break;
               case PHAuthorizationStatus.Denied:
                // Add code do run if user denied permission, if needed.
                    break;
               case PHAuthorizationStatus.Restricted:
                // Add code do run if user restricted permission, if needed.
                    break;
               default:
                    break;
        }
    });
    

    Now save your UIImage to the library with the following code (will not save any metadata, if any):

    imageData.SaveToPhotosAlbum((retImage, error) => {
          var o = retImage as UIImage;
          Console.WriteLine("error: " + error + "\nimage: " + o);
    });
    

    Hope this helps.

  • kalkotekedarkalkotekedar USMember ✭✭
    edited November 2017

    @JGoldberger thanks for your reply. It works for me to save the image to the gallery, but I can't get the path of the saved image.

  • kalkotekedarkalkotekedar USMember ✭✭

    @TedRogers said:
    @kalkotekedar Kind of hard to help since you don't describe the problem you are seeing. You will have to add code to make sure the "AppPhoto" directory has been created but otherwise you would have to describe the problem you are seeing in detail.

    My problem is once I saved a photo in a gallery, I want the path of the saved photo.

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    @kalkotekedar said:
    thanks for your reply. It works for me to save the image to the gallery, but I can't get the path of the saved image.

    That is because photos in the gallery do not have a "path" per se. You can not access photos in the gallery with normal File IO methods, especially because these days the file in the photo gallery may not even be stored on the local device. There was a discussion about this here: https://forums.xamarin.com/discussion/62772/uiimagepicker-get-path-from-selected-image-from-gallery

    So you have to use the PhotoKit framework and the PHAsset / PHImageManager and related classes. See this blog post for more info:
    https://blog.xamarin.com/build-great-photo-experiences-in-ios-8-with-photokit/

    Note that the above blog post is old, so you can ignore this sentence: "To use PhotoKit from Xamarin.iOS, you can download the preview release in our alpha channel." This was true in 2014, but now PhotoKit is available in the stable channel.

Sign In or Register to comment.