问题描述
在iOS 8的Photos.framework中,我正在尝试将UIImage保存到用户的照片库,就像在Safari中长按图像并选择保存图像一样。在iOS 7中,这只需要调用ALAssetLibrary的 writeImageToSavedPhotosAlbum:metadata:completionBlock
。
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
.
对于iOS 8,我们需要选择一个资产集合来添加一个PHAsset,但我无法弄清楚哪个PHAssetCollection最接近只保存到用户的相机滚动(即使没有一个,真的,在iOS 8中)
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.
推荐答案
你不需要乱用PHAssetCollection。只需添加:
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#>
}
}];
这篇关于哪个PHAssetCollection用于保存图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!