2014-09-26 55 views
17

在iOS 8的Photos.framework中,我试图将UIImage保存到用户的照片库中,这与您可以在Safari中长按图像并选择“保存图像”的方式非常相似。在iOS 7中,这可以通过调用ALAssetLibrary的writeImageToSavedPhotosAlbum:metadata:completionBlock来完成。哪个PHAssetCollection用于保存图像?

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

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

回答

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

在完成处理程序中,您如何获得新资产? – 2014-12-29 04:34:40

+1

根据文档,您可以使用@property(nonatomic,strong,readonly)PHObjectPlaceholder * placeholderForCreatedAsset',如果您需要引用由同一个变更块中的变更请求创建的资产,请使用此属性。以下代码在包含在照片库更改区块中时创建资产,然后将其添加到集合中:“ 有关占位符的更多信息,我建议您阅读[文档](https://developer.apple.com /library/prerelease/ios/documentation/Photos/Reference/PHObjectPlaceholder_Class/index.html)。 – 2014-12-29 15:41:18

+0

我已经尝试过占位符对象。该示例将占位符对象添加到相册更改请求中,但是仍在维护一组资源。将占位符对象添加到数组不起作用。 – 2014-12-29 15:51:30

6

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

+0

顺便说一句,你已经很好地理解了占位符是什么。 – matt 2014-11-13 18:00:22

+0

我想如果他尝试将它添加到该'ALAssetCollection',它仍然是只读的,无法添加。只要在我的回答中添加它就可以做到他想要的。 – 2014-11-13 18:07:59

+0

@SushiGrassJacob相机胶卷是可写的。 – matt 2014-11-13 19:01:31