18

在 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);
}];

我尝试访问“最近添加”智能相册,但它不允许向其中添加新内容。

4

2 回答 2

30

你不需要弄乱 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#>
        }
    }];
于 2014-11-13T17:54:38.147 回答
6

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

于 2014-11-13T17:59:13.200 回答