Harshad Pansuriya
Harshad Pansuriya

Reputation: 20910

Save UIImage as jpg File in Photo Library Directory in Xamarin.iOS

As I know this is Simple Approch to save it in a Photo Library. But It can save with custom filename.

var someImage = UIImage.FromFile("someImage.jpg");
someImage.SaveToPhotosAlbum((image, error) => {
    var o = image as UIImage;
    Console.WriteLine("error:" + error);
})

But I want to save it with filename.jpg in the Photo Library.

I try so much code but nothing is getting help to me.

Code 1 :

var imageName = "/" + dicomId.ToString() + ".jpg";

var documentsDirectory = Environment.GetFolderPath
                    (Environment.SpecialFolder.Personal);

string jpgFilename = System.IO.Path.Combine(documentsDirectory, imageName); // hardcoded filename, overwritten each time
NSData imgData = dicomImage.AsJPEG();
NSError err = null;
if (imgData.Save(jpgFilename, false, out err))
{
    Console.WriteLine("saved as " + jpgFilename);
}
else
{
    Console.WriteLine("NOT saved as " + jpgFilename + " because" + err.LocalizedDescription);
}

This code part goes to if condition but it can not save the Image.

Code 2 :

If using this part of Code

var documentsDirectoryPath = NSSearchPath.GetDirectories(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User, true)[0];

It give you don't have permission to save image.

I try lots of thing on google and SO but nothing could help to me.

Edit :

info.plist

enter image description here

Any Help would be Appreciated.

Upvotes: 1

Views: 3041

Answers (1)

Cheesebaron
Cheesebaron

Reputation: 24460

How about using UIImage.SaveToPhotosAlbum()?

Usage is something like:

image.SaveToPhotosAlbum((uiImage, nsError) =>
{
    if (nsError != null)
        // do something about the error..
    else
        // image should be saved
});

Make sure that you have requested permissions before you try to save.

PHPhotoLibrary.RequestAuthorization(status =>
{
    switch (status)
    {
        case PHAuthorizationStatus.Restricted:
        case PHAuthorizationStatus.Denied:
            // nope you don't have permission
            break;
        case PHAuthorizationStatus.Authorized:
            // yep it is ok to save
            break;
    }
});

Edit: if you want more control, you need to use PHPhotosLibrary, which is an awful API...

var library = PHPhotoLibrary.SharedPhotoLibrary;

var albumName = "MyPhotos";

var fetchOptions = new PHFetchOptions();
fetchOptions.Predicate = NSPredicate.FromFormat($"title = {albumName}");
var assetsCollections = PHAssetCollection.FetchAssetCollections(
    PHAssetCollectionType.Album, PHAssetCollectionSubtype.Any, fetchOptions);

var collection = assetsCollections.firstObject as PHAssetCollection;

library.PerformChanges(() => {
    var options = new PHAssetResourceCreationOptions();
    options.OriginalFilename = "filename.jpg";
    var createRequest = PHAssetCreationRequest.CreationRequestForAsset();
    createRequest.AddResource(PHAssetResourceType.FullSizePhoto, image.AsJPEG(1), options);

    // if you want to save to specific album... otherwise just remove these three lines
    var placeholder = createRequest.PlaceholderForCreatedAsset;
    var albumChangeRequest = PHAssetCollectionChangeRequest.ChangeRequest(collection);
    albumChangeRequest.AddAssets(new PHObject[] { placeholder });
}, 
(ok, error) => {
    if (error != null)
    {
        // someone set up us the bomb
    }
});

Upvotes: 1

Related Questions