Guardar una foto en un álbum personalizado en iOS 8

Necesito un poco de ayuda aquí, tengo un método que guarda un UIImage en la camera sin problemas en iOS 8. El método es el siguiente

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

Necesito adaptar ese código, para que la image en lugar de save el UIImage en el rollo de la camera, guarde en un álbum personalizado llamado "MyAlbum"

Estoy usando el Photos.framework

Primero necesitará verificar que el álbum exista con una request de captura, y luego agregar la image al álbum o crear el álbum y luego agregar la image.

 #import <Photos/Photos.h> - (void)addImageToCameraRoll:(UIImage *)image { NSString *albumName = @"MyAlbum"; void (^saveBlock)(PHAssetCollection *assetCollection) = ^void(PHAssetCollection *assetCollection) { [[PHPhotoLibrary shanetworkingPhotoLibrary] 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.pnetworkingicate = [NSPnetworkingicate pnetworkingicateWithFormat:@"localizedTitle = %@", albumName]; PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions]; if (fetchResult.count > 0) { saveBlock(fetchResult.firstObject); } else { __block PHObjectPlaceholder *albumPlaceholder; [[PHPhotoLibrary shanetworkingPhotoLibrary] 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); } }]; } } 

Cree un nuevo álbum con el nombre "MyAlbum" antes de agregar un elemento en el álbum.

  // Create new album. __block PHObjectPlaceholder *albumPlaceholder; [[PHPhotoLibrary shanetworkingPhotoLibrary] 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 shanetworkingPhotoLibrary] 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); } }]; 

En su bloque de cambios, especifique la colección de activos de la request

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

Comtesting si el álbum ya existe.

  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 }