我在新的PHPhotoLibrary中找不到与ALAssetsLibrary-> writeImageDataToSavedPhotosAlbum类似的方法,因为在iOS 9中不推荐使用ALAssetsLibrary我无法保存GIF可能我正在使用此代码
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[UIImage imageWithData:gifdata]];
placeholder = [assetRequest placeholderForCreatedAsset];
photosAsset = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection
assets:photosAsset];
[albumChangeRequest addAssets:@[placeholder]];
} completionHandler:^(BOOL success, NSError *error) {
if (success)
{
}
else
{
NSLog(@"%@", error);
}
}];
答案 0 :(得分:8)
我使用NSData
来保存gif图片,在iOS8之后弃用了ALAssetsLibrary
方法UIImageWriteToSavedPhotosAlbum
,api说我们可以使用
PHAssetCreationRequest
方法[[PHAssetCreationRequest creationRequestForAsset] addResourceWithType:PHAssetResourceTypePhoto data:data options:options];
保存此gif图像数据,因此您也可以使用网址请求来保存GIF
代码如下:
NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"gif"];
NSData *data = [NSData dataWithContentsOfFile:path];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
[[PHAssetCreationRequest creationRequestForAsset] addResourceWithType:PHAssetResourceTypePhoto data:data options:options];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
NSLog(@":%d",success);
}];
答案 1 :(得分:2)
我已通过使用Photos Framework将gif文件成功保存到相册中
PHPhotoLibrary.shared().performChanges ({
let url = URL(fileURLWithPath: "your file path")
PHAssetChangeRequest.creationRequestForAssetFromImage(atFileURL: url)
}) { saved, error in
if saved {
print("Your image was successfully saved")
}
}
希望获得帮助!
答案 2 :(得分:1)
我通过创建临时文件并使用:
解决了我的问题creationRequestForAssetFromImageAtFileURL