0

I'm trying to create a Photo "folder" and return a pointer to it. Is this possible? Below is the code I have so far. Thanks.

- (PHCollectionList *) CreatePhotoFolder: (NSString *) folderName
{
    [[PHPhotoLibrary sharedPhotoLibrary]
        performChanges:^{
            // Create the folder
            [PHCollectionListChangeRequest creationRequestForCollectionListWithTitle: folderName];
        }
        completionHandler:^(BOOL success, NSError *error) {
            if (!success) { NSLog(@"Error creating Folder: %@", error); }
            else {
                // How do I return the new PHCollectionList* for the new folder?
            }
        }
     ];
}
NoBot
  • 198
  • 1
  • 7

1 Answers1

0

You need to get the local ID by placeholder first.

    __block NSString * localId;
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"XD"];
        localId = [[assetCollectionChangeRequest placeholderForCreatedAssetCollection] localIdentifier];
    } completionHandler:^(BOOL success, NSError *error) {
        if (!success) {
            NSLog(@"Error creating album: %@", error);
        } else {
            NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
            [prefs setObject:localId forKey:@"collection"];
            [prefs synchronize];
        }
    }];

and then get the PHAssetCollection by this local ID

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString* colletionId = [prefs stringForKey:@"collection"];
if(colletionId == nil) {
    colletionId = @"";
}

PHFetchResult *result = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[colletionId]  options:nil];
self.assetCollection = [result firstObject];
Rigel Chen
  • 861
  • 7
  • 14
  • Thank you! I was able to adapt your sample for my need. Is there a reason you're storing the localId into the *prefs vs simply returning a pointer using [result firstObject]? – NoBot Sep 28 '15 at 21:43
  • I store it because I need to use it in other place. You can use the pointer directly. – Rigel Chen Sep 29 '15 at 11:45