Saving a photo to a custom album in iOS 8

I need a little help here, I have a method that saves UIImage for camera roll without problems in iOS 8. This method is as follows

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ [PHAssetChangeRequest creationRequestForAssetFromImage:image]; }completionHandler:^(BOOL success, NSError *error) { if(success){ NSLog(@"worked"); }else{ NSLog(@"Error: %@", error); } }]; 

I need to adapt this code so that instead of saving the UIImage in the camera frame, it is saved in a user album called "MyAlbum"

I am using Photos.framework

+6
source share
4 answers

First you need to check if there is an album with a selection request, and then either add the image to the album, or create an album and then add the image.

 #import <Photos/Photos.h> - (void)addImageToCameraRoll:(UIImage *)image { NSString *albumName = @"MyAlbum"; void (^saveBlock)(PHAssetCollection *assetCollection) = ^void(PHAssetCollection *assetCollection) { [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image]; PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection]; [assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]]; } completionHandler:^(BOOL success, NSError *error) { if (!success) { NSLog(@"Error creating asset: %@", error); } }]; }; PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init]; fetchOptions.predicate = [NSPredicate predicateWithFormat:@"localizedTitle = %@", albumName]; PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions]; if (fetchResult.count > 0) { saveBlock(fetchResult.firstObject); } else { __block PHObjectPlaceholder *albumPlaceholder; [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:albumName]; albumPlaceholder = changeRequest.placeholderForCreatedAssetCollection; } completionHandler:^(BOOL success, NSError *error) { if (success) { PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[albumPlaceholder.localIdentifier] options:nil]; if (fetchResult.count > 0) { saveBlock(fetchResult.firstObject); } } else { NSLog(@"Error creating album: %@", error); } }]; } } 
+11
source

Create a new album called "MyAlbum" before adding the asset to the album.

  // Create new album. __block PHObjectPlaceholder *albumPlaceholder; [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ PHAssetCollectionChangeRequest *changeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title]; albumPlaceholder = changeRequest.placeholderForCreatedAssetCollection; } completionHandler:^(BOOL success, NSError *error) { if (success) { PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[albumPlaceholder.localIdentifier] options:nil]; PHAssetCollection *assetCollection = fetchResult.firstObject; // Add it to the photo library [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image]; PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection]; [assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]]; } completionHandler:^(BOOL success, NSError *error) { if (!success) { NSLog(@"Error creating asset: %@", error); } }]; } else { NSLog(@"Error creating album: %@", error); } }]; 
+7
source

In your change block, you specify an assetCollection request

let request = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(-YOUR ALBUM NAME-)

0
source

Check if an album exists.

  NSString *localIdentifier; PHFetchResult<PHAssetCollection *> *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]; for (PHAssetCollection *assetCollection in assetCollections) { if([[assetCollection localizedTitle] isEqualToString:album] ){ localIdentifier = assetCollection.localIdentifier; break; } } if(localIdentifier ){ ///fetch album PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[localIdentifier] options:nil]; }else{ ///creat album here } 
0
source

Source: https://habr.com/ru/post/987239/


All Articles