Which PHAssetCollection to use for saving an image?

痞子三分冷 提交于 2019-11-27 02:38:15

问题


In iOS 8's Photos.framework, I'm trying to save a UIImage to the user's photo library, much in the same way you can long-press an image in Safari and choose the "Save Image". In iOS 7, this would be by just calling ALAssetLibrary's writeImageToSavedPhotosAlbum:metadata:completionBlock.

For iOS 8 we need to pick an asset collection to add a PHAsset to, but I can't figure out which PHAssetCollection is closest to just saving to the user's "camera roll" (even though there isn't one, really, in 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);
}];

I tried accessing the "Recently Added" smart album, but it does not allow adding new content to it.


回答1:


You don't need to mess around with PHAssetCollection. Just add by doing:

    [[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#>
        }
    }];



回答2:


Actually in 8.1 the Camera Roll is back. It is the smart album whose subtype is SmartAlbumUserLibrary.



来源:https://stackoverflow.com/questions/26065774/which-phassetcollection-to-use-for-saving-an-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!