如何在创建后获得指向新PHCollectionList(或PHAssetCollection)的指针

时间:2015-08-28 21:54:06

标签: ios ios8 photokit phphotolibrary

我试图创建一个Photo"文件夹"并返回指向它的指针。这可能吗?下面是我到目前为止的代码。感谢。

- (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?
            }
        }
     ];
}

1 个答案:

答案 0 :(得分:0)

您需要先按占位符获取本地ID。

    __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];
        }
    }];

然后通过此本地ID获取PHAssetCollection

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

PHFetchResult *result = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[colletionId]  options:nil];
self.assetCollection = [result firstObject];