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.