变更记录
序号 | 录入时间 | 录入人 | 备注 |
---|---|---|---|
1 | 2016-12-01 | Alfred Jiang | - |
方案名称
相册 - 保存 GIF 动画图片至相册
关键字
相册 \ GIF \ 动图保存
需求场景
- 需要保存 GIF 图片至相册时
参考链接
- 伯乐在线 - (推荐)
详细内容
引入头文件
#import <Photos/Photos.h>
#import <AssetsLibrary/AssetsLibrary.h>
保存函数如下
- (void)saveImageData:(NSData *)imageData completionBlock:(void (^)(NSError *error))completionBlock
{
if ([UIDevice currentDevice].systemVersion.floatValue >= 9.0f) {
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
[[PHAssetCreationRequest creationRequestForAsset] addResourceWithType:PHAssetResourceTypePhoto
data:imageData
options:options];
}
completionHandler:^(BOOL success, NSError * _Nullable error) {
if (completionBlock) {
completionBlock(error);
}
}];
}
else {
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageDataToSavedPhotosAlbum:imageData
metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error) {
if (completionBlock) {
completionBlock(error);
}
}];
}
}
使用示例
NSString *path = [[NSBundle mainBundle] pathForResource:@"testGIFImage" ofType:@"gif"];
NSData *data = [NSData dataWithContentsOfFile:path];
[self saveImageData:data
completionBlock:^(NSError *error) {
if (error) {
NSLog(@"error : %@",error);
}
}];
效果图
(无)
备注
(无)