0

我正在尝试创建一个照片“文件夹”并返回一个指向它的指针。这可能吗?以下是我到目前为止的代码。谢谢。

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

1 回答 1

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];
于 2015-09-16T02:55:21.223 回答