question

SWAPNILGAIKWAD-3135 avatar image
0 Votes"
SWAPNILGAIKWAD-3135 asked RobCaplan edited

how to store captured image in image album in Xamarin.Forms for ios


how to store captured image in an image album in Xamarin.Forms for ios?
I am using xamarin essential library for capture photo... please help me

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

KyleWang-MSFT avatar image
0 Votes"
KyleWang-MSFT answered

Hi SWAPNILGAIKWAD-3135,

Welcome to our Microsoft Q&A platform!

To save the photo to album, you can create a DependencyService for iOS. More info about DependencyService, you can refer to Xamarin.Forms DependencyService.

Here is the demo.

ISavePhotoService.cs

 public interface ISavePhotoService
 {
     void SaveImageFromStream(Stream imageStream, string filename);
 }

SavePhotoService.cs

 [assembly: Dependency(typeof(SavePhotoService))]
 namespace SavePhotoToAlbum.iOS
 {
     class SavePhotoService : ISavePhotoService
     {
         public void SaveImageFromStream(Stream imageStream, string fileName)
         {
             var imageData = new UIImage(NSData.FromStream(imageStream));
             imageData.SaveToPhotosAlbum((image, error) =>
             {
                 if (error != null)
                 {
                     Console.WriteLine(error.ToString());
                 }
             });
         }
     }
 }

Then pass the Stream type return value of MediaPicker.CapturePhotoAsync() to SaveImageFromStream().

 class MainPageViewModel
 {
     public ICommand TakePhotoCommand { get; private set; }
    
     public MainPageViewModel()
     {
         TakePhotoCommand = new Command(async () => await TakePhotoAsync());
     }
    
     async Task TakePhotoAsync()
     {
         try
         {
             var photo = await MediaPicker.CapturePhotoAsync();
             await SavePhotoAsync(photo);
         }
         catch (Exception ex)
         {
             Console.WriteLine($"CapturePhotoAsync THREW: {ex.Message}");
         }
     }
    
     async Task SavePhotoAsync(FileResult photo)
     {
         // canceled
         if (photo == null)
         {
             return;
         }
         // save to album
         using (var stream = await photo.OpenReadAsync())
         {
             DependencyService.Get<ISavePhotoService>().SaveImageFromStream(stream, "test.png");
         }
     }
 }

Besides, you can also use the Nuget package jamesmontemagno/MediaPlugin, which provides property "SaveToAlbum".

 var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
 {
     SaveToAlbum = true
 });

Regards,
Kyle


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.