Search code examples
iosobjective-calassetslibraryavassetwriterphasset

Putting videos in one specific Album in camera roll


I am creating a specific album in my camera roll.. But for my every run, its creating a new album in my camera roll instead of putting the videos together to the same album.. Ideas?

__block PHObjectPlaceholder *assetCollectionPlaceholder;

    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{


        // Create new album.
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            PHAssetCollectionChangeRequest *createAlbumRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"Eye Movement"];
            assetCollectionPlaceholder = createAlbumRequest.placeholderForCreatedAssetCollection;
        } completionHandler:^(BOOL success, NSError *error) {
            if (success) {
                PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[assetCollectionPlaceholder.localIdentifier] options:nil];
                PHAssetCollection *assetCollection = fetchResult.firstObject;

                // Add it to the photo library
                [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                    PHAssetChangeRequest* createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:outputFileURL];
                    PHAssetCollectionChangeRequest* assetRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
                     [assetRequest addAssets:@[[createAssetRequest placeholderForCreatedAsset]]];
                    //PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
                    //[assetCollectionChangeRequest addAssets:@[[createAssetRequest placeholderForCreatedAsset]]];
                } completionHandler:^(BOOL success, NSError *error) {
                    if (!success) {
                        NSLog(@"Error creating asset: %@", error);
                    }
                }];
            } else {
                //NSLog(@"Error creating album: %@", error);
                NSLog(@"didFinishRecordingToOutputFileAtURL - success for ios9");
            }
        }];

Solution

  • You have to check to see if the album is created first, then tell the app to create the album if it does not exist.

    __block PHFetchResult *photosAsset;
    __block PHAssetCollection *collection;
    __block PHObjectPlaceholder *placeholder;
    
    // Find the album
    PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
    fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", @"Your Album Name Here"];
    collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
                                                          subtype:PHAssetCollectionSubtypeAny
                                                          options:fetchOptions].firstObject;
    // Create the album
    if (!collection)
    {
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            PHAssetCollectionChangeRequest *createAlbum = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"Your Album Name Here"];
            placeholder = [createAlbum placeholderForCreatedAssetCollection];
        } completionHandler:^(BOOL success, NSError *error) {
            if (success)
            {
                PHFetchResult *collectionFetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[placeholder.localIdentifier]
                                                                                                            options:nil];
                collection = collectionFetchResult.firstObject;
            }
        }];
    }