哪个PHAssetCollection用于保存图像?

Riz*_*tar 17 objective-c ios ios8 photokit

在iOS 8的Photos.framework中,我正在尝试将UIImage保存到用户的照片库,就像在Safari中长按图像并选择"保存图像"一样.在iOS 7中,这只需要调用ALAssetLibrary writeImageToSavedPhotosAlbum:metadata:completionBlock.

对于iOS 8,我们需要选择一个资产集合来添加PHAsset,但我无法弄清楚哪个PHAssetCollection最接近只保存到用户的"相机滚动"(即使在iOS中没有,真的, 8)

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    PHAssetChangeRequest *newAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
    newAssetRequest.creationDate = time;
    newAssetRequest.location = location;

    PHObjectPlaceholder *placeholderAsset = newAssetRequest.placeholderForCreatedAsset;

    PHAssetCollectionChangeRequest *addAssetRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:WHICH_ASSET_COLLECTION];
    addAssetRequest addAssets:@[placeholderAsset];

} completionHandler:^(BOOL success, NSError *error) {
    NSLog(@"Success: %d", success);
}];
Run Code Online (Sandbox Code Playgroud)

我尝试访问"最近添加"的智能相册,但不允许添加新内容.

Sus*_*cob 29

你不需要乱用PHAssetCollection.只需添加:

    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:<#your photo here#>];
    } completionHandler:^(BOOL success, NSError *error) {
        if (success) {
            <#your completion code here#>
        }
        else {
            <#figure out what went wrong#>
        }
    }];
Run Code Online (Sandbox Code Playgroud)

  • 很好的触摸添加占位符令牌到您的示例代码! (2认同)

mat*_*att 6

实际上在8.1中,相机胶卷又回来了.它是智能相册,其子类型为SmartAlbumUserLibrary.