0

I am using the following code to attempt to save a new image to a PHAssetCollection, specifically, the Camera Roll (aka User Library) :

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];

    PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
    PHAssetCollection *assetCollection = fetchResult[0];

    if (assetCollection) {
        PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
        [assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];
    }
} completionHandler:^(BOOL success, NSError *error) {
    if (!success) {
        NSLog(@"Error creating asset: %@", error);
    }
}];

I always get an error.

All of the objects in the perform block look fine:

(lldb) po image
<UIImage: 0x174289ec0>, {1080, 1466}

(lldb) po assetCollection
<PHAssetCollection: 0x1741d5540> F6705124-D49B-4FDC-9191-7E84CFCCD148/L0/040 Camera Roll assetCollectionType=2/209

(lldb) po assetCollectionChangeRequest
<PHAssetCollectionChangeRequest: 0x170264640> title=(null) hasAssetChanges=1

And the error message is pretty useless:

The operation couldn’t be completed. (Cocoa error -1.)

How can I successfully save my new image to the user's library? Thanks.

mkc842
  • 2,361
  • 2
  • 26
  • 38

1 Answers1

2

In general you're doing things in the wrong order; you should not be doing any fetching inside a performChanges block. And you don't have to, in any case. Do not fetch the collection at all. Just create the photo, plain and simple, exactly as in your first line - except that you don't even need to keep a reference to the change request:

[PHAssetChangeRequest creationRequestForAssetFromImage:image];

...and stop. At that point the photo has been added to the camera roll.

I just tried this and it works perfectly.

(Of course I'm assuming you have already obtained the necessary permissions from the user...!)

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks, matt. I got confused by http://stackoverflow.com/questions/26065774/which-phassetcollection-to-use-for-saving-an-image – mkc842 Jul 03 '15 at 15:53
  • Yes, but that's the _question_. He's having trouble because he's doing it wrong — just like you. The _answer_ is what you want to look at: http://stackoverflow.com/a/26915282/341994 In fact, that's where I got my answer from! (I did then test it for myself, of course.) – matt Jul 03 '15 at 17:02