iOS11中iOS处理GIF图片的方式
GIF 五部走如下 :
1 從相冊中取出GIF圖的Data
2 通過騰訊的IM發送Gif圖
3 展示GIF圖
4 GIF圖URL緩存機制
5 將展示的GIF圖存到相冊中
一 從相冊中取出GIF圖中的Data
1.TZImagePickerController中利用方法來獲取到gif圖片的image和asses
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingGifImage:(UIImage *)animatedImage sourceAssets:(id)asset
2.通過如下方法判斷是否是gif格式:
if ([[asset valueForKey:@"filename"] tz_containsString:@"GIF"])
3.如果是gif圖片格式,通過 PHImageManager 的方法利用字段assets來獲取gif動圖的data數據
二通過騰訊的IM發送Gif圖
1.將gif的數據存到臨時文件夾
NSString *tempDir = NSTemporaryDirectory();
NSString *snapshotPath = [NSStringstringWithFormat:@"%@%3.f%@.gif", tempDir, [NSDatetimeIntervalSinceReferenceDate],[[NSProcessInfoprocessInfo] globallyUniqueString]];
NSError *err;
NSFileManager *fileMgr = [NSFileManagerdefaultManager];
if (![fileMgr createFileAtPath:snapshotPath contents:imageData attributes:nil])
{
DebugLog(@"Upload Image Failed: fail to create uploadfile: %@", err);
}
2.封裝成消息體發送
TIMMessage * msg = [[TIMMessagealloc] init];
TIMImageElem * image_elem = [[TIMImageElemalloc] init];
image_elem.path = snapshotPath;
image_elem.format = TIM_IMAGE_FORMAT_GIF;
image_elem.level = TIM_IMAGE_COMPRESS_ORIGIN;
[msg addElem:image_elem];
@weakify(self);
[self.conversationsendMessage:msg succ:^() {
@strongify(self);
//IDSPostNFWithObj(kIMPartyRoomSendMessage,msg);
[selfforceUpdataTableView];
NSLog(@"ddsuccess");
} fail:^(int code, NSString *err) {
NSLog(@"ddfailed");
}];
三展示GIF圖
1.在 MJPhoto 中本地路徑展示方式:
MJPhoto *photo = [[MJPhotoalloc] init];
NSData *data = [NSDatadataWithContentsOfFile:picModel.picPath];
photo.photodata = data;
photo.image = [UIImagesd_tz_animatedGIFWithData:data];
2.在 MJPhoto 遠程url路徑展示方式:
photo.image = [UIImagesd_tz_animatedGIFWithData:[NSDatadataWithContentsOfURL:[NSURLURLWithString:imageURL]]];
3.在 cell 中使用 FLAnimatedImageView展示方式:
[self.animatedImageViewsd_setImageWithURL:imageURL];
Ps:前提是需要更新SDWebImage版本,需要有FLAnimatedImageView+WebCache 文件
四GIF圖URL緩存機制
1.通過 gifURL 加入緩存機制:
NSURL *newUrl = [NSURLURLWithString:imageURL];
[[SDWebImageDownloadersharedDownloader] downloadImageWithURL:newUrl
options:0
progress:nil
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
[[[SDWebImageManagersharedManager] imageCache] storeImage:image imageData:data forKey:newUrl.absoluteStringtoDisk:YEScompletion:^{
photo.image = [UIImagesd_tz_animatedGIFWithData:data];
photo.photodata = data;
}];
}];
}
- (NSData *)imageDataFromDiskCacheWithKey:(NSString *)key
{
NSString *path = [[[SDWebImageManagersharedManager] imageCache] defaultCachePathForKey:key];
return [NSDatadataWithContentsOfFile:path];
}
五 將展示的GIF圖存到相冊中
if ([UIDevicecurrentDevice].systemVersion.floatValue >= 9.0f) {
[[PHPhotoLibrarysharedPhotoLibrary] performChanges:^{
PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptionsalloc] init];
[[PHAssetCreationRequestcreationRequestForAsset] addResourceWithType:PHAssetResourceTypePhotodata:photo.photodataoptions:options];
} completionHandler:^(BOOL success, NSError * _Nullable error)
if (success) {
。。。
}
else {
。。。
}
}];
}
else {
UIImageWriteToSavedPhotosAlbum(photo.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
思考與行動:
1.寫出 Gif格式的 URL 轉換成 GIF格式的data數據類型的轉換函數
2.寫出 Gif格式的 UIImage 轉換成 GIF格式的data數據類型的轉換函數
3.寫出Gif格式的data數據類型的轉換成 GIF格式的UIImage的轉換函數
4.寫出 Gif格式的 Assets 轉換成 GIF格式的data數據類型的轉換函數
======
附錄:
+ (UIImage *)sd_tz_animatedGIFWithData:(NSData *)data {
if (!data) {
returnnil;
}
CGImageSourceRef source = CGImageSourceCreateWithData((__bridgeCFDataRef)data, NULL);
size_t count = CGImageSourceGetCount(source);
UIImage *animatedImage;
if (count <= 1) {
animatedImage = [[UIImagealloc] initWithData:data];
}
else {
NSMutableArray *images = [NSMutableArrayarray];
NSTimeInterval duration = 0.0f;
for (size_t i = 0; i < count; i++) {
CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
if (!image) {
continue;
}
duration += [selfsd_frameDurationAtIndex:i source:source];
[images addObject:[UIImageimageWithCGImage:image scale:[UIScreenmainScreen].scaleorientation:UIImageOrientationUp]];
CGImageRelease(image);
}
if (!duration) {
duration = (1.0f / 10.0f) * count;
}
animatedImage = [UIImageanimatedImageWithImages:images duration:duration];
}
CFRelease(source);
return animatedImage;
}
...
總結
以上是生活随笔為你收集整理的iOS11中iOS处理GIF图片的方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 招行信用卡青年卡取现手续费 小心高额取现
- 下一篇: 哥味文学是什么梗上网络热搜:原来与周杰伦