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 = [ NSString stringWithFormat : @"%@%3.f%@.gif" , tempDir, [ NSDate timeIntervalSinceReferenceDate ],[[ NSProcessInfo processInfo ] globallyUniqueString ]];
            NSError *err;
            NSFileManager *fileMgr = [ NSFileManager defaultManager ];
            if (![fileMgr createFileAtPath :snapshotPath contents :imageData attributes : nil ])
            {
                DebugLog ( @"Upload Image Failed: fail to create uploadfile: %@" , err);
            }
 
2.封装成消息体发送
 
            TIMMessage * msg = [[ TIMMessage alloc ] init ];
            TIMImageElem * image_elem = [[ TIMImageElem alloc ] 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 . conversation sendMessage :msg succ :^() {
                @ strongify ( self );
                //IDSPostNFWithObj(kIMPartyRoomSendMessage,msg);
                [ self forceUpdataTableView ];
                NSLog ( @"ddsuccess" );
               
            } fail :^( int code, NSString *err) {
                NSLog ( @"ddfailed" );
            }];
 
 
三 展示GIF图
 
1.在 MJPhoto 中本地路径展示方式:
 
MJPhoto *photo = [[ MJPhoto alloc ] init ];
NSData *data = [ NSData dataWithContentsOfFile :picModel. picPath ];
photo. photodata = data;
photo. image = [ UIImage sd_tz_animatedGIFWithData :data];
 
2.在 MJPhoto 远程url路径展示方式:
 
photo. image = [ UIImage sd_tz_animatedGIFWithData :[ NSData dataWithContentsOfURL :[ NSURL URLWithString :imageURL]]];
 
3.在 cell 中使用 FLAnimatedImageView 展示方式:
 
[ self . animatedImageView sd_setImageWithURL : imageURL ];
 
Ps:前提是需要更新SDWebImage版本,需要有 FLAnimatedImageView+WebCache 文件
 
四 GIF图URL缓存机制
 
1.通过 gifURL 加入缓存机制:
 
                    NSURL *newUrl = [ NSURL URLWithString :imageURL];
                    [[ SDWebImageDownloader sharedDownloader ] downloadImageWithURL :newUrl
                                                                          options : 0
                                                                         progress : nil
                                                                        completed :^( UIImage *image, NSData *data, NSError *error, BOOL finished) {
                                                                            [[[ SDWebImageManager sharedManager ] imageCache ] storeImage :image imageData :data forKey :newUrl. absoluteString toDisk : YES completion :^{
                                                                                photo. image = [ UIImage sd_tz_animatedGIFWithData :data];
                                                                                photo. photodata = data;
                                                                            }];
                                                                        }];
                }
 
- ( NSData *)imageDataFromDiskCacheWithKey:( NSString *)key
{
    NSString *path = [[[ SDWebImageManager sharedManager ] imageCache ] defaultCachePathForKey :key];
    return [ NSData dataWithContentsOfFile :path];
}
 
 
五 将展示的GIF图存到相册中
 
 
            if ([ UIDevice currentDevice ]. systemVersion . floatValue >= 9.0f ) {
                [[ PHPhotoLibrary sharedPhotoLibrary ] performChanges :^{
                    PHAssetResourceCreationOptions *options = [[ PHAssetResourceCreationOptions alloc ] init ];
                    [[ PHAssetCreationRequest creationRequestForAsset ] addResourceWithType : PHAssetResourceTypePhoto data :photo. photodata options :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) {
        return nil ;
    }
   
    CGImageSourceRef source = CGImageSourceCreateWithData (( __bridge CFDataRef )data, NULL );
   
    size_t count = CGImageSourceGetCount (source);
   
    UIImage *animatedImage;
   
    if (count <= 1 ) {
        animatedImage = [[ UIImage alloc ] initWithData :data];
    }
    else {
        NSMutableArray *images = [ NSMutableArray array ];
       
        NSTimeInterval duration = 0.0f ;
       
        for ( size_t i = 0 ; i < count; i++) {
            CGImageRef image = CGImageSourceCreateImageAtIndex (source, i, NULL );
            if (!image) {
                continue ;
            }
           
            duration += [ self sd_frameDurationAtIndex :i source :source];
           
            [images addObject :[ UIImage imageWithCGImage :image scale :[ UIScreen mainScreen ]. scale orientation : UIImageOrientationUp ]];
           
            CGImageRelease (image);
        }
       
        if (!duration) {
            duration = ( 1.0f / 10.0f ) * count;
        }
       
        animatedImage = [ UIImage animatedImageWithImages :images duration :duration];
    }
   
    CFRelease (source);
   
    return animatedImage;
}
 
...

转载于:https://www.cnblogs.com/firstrate/p/7694203.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值