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

S GAIKWAD 231 Reputation points
2021-08-11T12:41:27.12+00:00

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

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,363 questions
0 comments No comments
{count} votes

Accepted answer
  1. Kyle Wang 5,531 Reputation points Microsoft Vendor
    2021-08-12T02:39:57.68+00:00

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.