我正在使用以下代码尝试将新图像保存到一个PHAssetCollection
,特别是相机胶卷(又名用户库):
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
PHAssetCollection *assetCollection = fetchResult[0];
if (assetCollection) {
PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
[assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];
}
} completionHandler:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"Error creating asset: %@", error);
}
}];
我总是得到一个错误。
执行块中的所有对象看起来都很好:
(lldb) po image
<UIImage: 0x174289ec0>, {1080, 1466}
(lldb) po assetCollection
<PHAssetCollection: 0x1741d5540> F6705124-D49B-4FDC-9191-7E84CFCCD148/L0/040 Camera Roll assetCollectionType=2/209
(lldb) po assetCollectionChangeRequest
<PHAssetCollectionChangeRequest: 0x170264640> title=(null) hasAssetChanges=1
并且错误消息毫无用处:
The operation couldn’t be completed. (Cocoa error -1.)
我怎样才能成功地将我的新图像保存到用户的库中?谢谢。