哪个PHAssetCollection用于保存图像?

时间:2014-09-26 18:09:51

标签: ios objective-c ios8 photokit

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

对于iOS 8,我们需要选择一个资产集合来添加PHAsset,但是我无法确定哪个PHAssetCollection最接近只保存给用户"相机胶卷&#34 ; (尽管在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);
}];

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

2 个答案:

答案 0 :(得分: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#>
        }
    }];

答案 1 :(得分:6)

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