创建后如何获得指向新 PHCollectionList(或 PHAssetCollection)的指针
How do you get a pointer to a new PHCollectionList (or PHAssetCollection) after creating
我正在尝试创建照片 "folder" 和 return 指向它的指针。这可能吗?下面是我到目前为止的代码。谢谢
- (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?
}
}
];
}
您需要先通过占位符获取本地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];
我正在尝试创建照片 "folder" 和 return 指向它的指针。这可能吗?下面是我到目前为止的代码。谢谢
- (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?
}
}
];
}
您需要先通过占位符获取本地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];