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图片的方式的更多相关文章

  1. iOS11中navigationBar上 按钮图片设置frame无效 不受约束 产生错位问题 解决

    问题描述: 正常样式: 在iOS 11 iPhone X上显示效果: 观察顶部navBar上的左侧按钮  在ios 11 上  这个按钮的图片不受设置的尺寸约束,按其真实大小展示,造成图片错位,影响界 ...

  2. iOS开发中的4种数据持久化方式【二、数据库 SQLite3、Core Data 的运用】

                   在上文,我们介绍了ios开发中的其中2种数据持久化方式:属性列表.归档解档.本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3.Core Data 的运 ...

  3. iOS - 选取相册中iCloud云上图片和视频的处理

    关于iOS选取相册中iCloud云上图片和视频  推荐看:TZImagePickerController的源码,这个是一个非常靠谱的相册选择图片视频的库 .当然也可以自己写 如下遇到的问题 工作原因, ...

  4. IOS开发数据存储篇—IOS中的几种数据存储方式

    IOS开发数据存储篇—IOS中的几种数据存储方式 发表于2016/4/5 21:02:09  421人阅读 分类: 数据存储 在项目开发当中,我们经常会对一些数据进行本地缓存处理.离线缓存的数据一般都 ...

  5. ios中摄像头/相册获取图片压缩图片上传服务器方法总结

    本文章介绍了关于ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结,有需要了解的同学可以参考一下下.     这几天在搞iphone上面一个应用的开发,里面有需要摄像头/相册编程和图片上传的问 ...

  6. iOS开发之UITableView中计时器的几种实现方式(NSTimer、DispatchSource、CADisplayLink)

    最近工作比较忙,但是还是出来更新博客了.今天博客中所涉及的内容并不复杂,都是一些平时常见的一些问题,通过这篇博客算是对UITableView中使用定时器的几种方式进行总结.本篇博客会给出在TableV ...

  7. TIBCO Jasper Report 中显示图片的方式

    最近在做的项目中,需要输出很多报表类文档,于是选择用jasper来帮助完成. 使用jasper studio的版本是 :TIB_js-studiocomm_6.12.2_windows_x86_64. ...

  8. iOS:实现图片的无限轮播(二)---之使用第三方库SDCycleScrollView

    iOS:实现图片的无限轮播(二)---之使用第三方库SDCycleScrollView 时间:2016-01-19 19:13:43      阅读:630      评论:0      收藏:0   ...

  9. TuSDK 简易使用方法 持有图片对象方式

    TuSDK 为涂图照相应用的SDK,打包后文件大小约为5M,缺点为包比较大,且图片清晰度较差一些,优点为直接可以引用滤镜贴纸,方便易用.   使用方法如下:    1.AppDelegate.m 中加 ...

随机推荐

  1. Ultra-wideband (UWB) secure wireless device pairing and associated systems

    Methods and systems are disclosed for ultra-wideband (UWB) secure wireless device pairing. Secure pa ...

  2. 探究Promise的实现

    最终答案在一个类库里,地址 https://github.com/yahoo/ypromise 这个类库也有问题,就是下面这道面试题在IE9里实现不一致,类库里还是用了setTimeout.去年尝试用 ...

  3. mod_timer函数及其他定时器函数

    当一个定时器已经被插入到内核动态定时器链表中后,我们还能够改动该定时器的expires值.函数mod_timer()实现这一点 改动注冊入计时器列表的handler的起动时间 int mod_time ...

  4. hbase结合hive和sqoop实现数据指导mysql

    hive综合hbase两个优势表中的:     1.实现数据导入到MYSQL.     2.实现hbase表转换为另外一张hbase表.  三个操作环节:      1.hbase关联hive作为外部 ...

  5. Android Studio怎样提示函数使用方法

    Eclipse有一个非常好的功能,就是当你代码调用某个android API时,鼠标移到相应的函数或者方法上,就会自己主动有一个悬 浮窗提示该函数的说明(所包括的參数含义,该方法功能).迁移到Andr ...

  6. HDU 4279 Number(2012天津网络游戏---数论分析题)

    转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接:pid=4279">http://acm.hdu.edu ...

  7. Goutte 获取http response

    $client = new Goutte\Client(); $crawler = $client->request('GET', 'http://symfony.com'); 获取http 响 ...

  8. 【C/S通信交互之Http篇】Cocos2dx(Client)使用Curl与Jetty(Server)实现手机网游Http通信框架(内含解决curl.h头文件找不到问题)

    之前已经分享过一篇基于Cocos2dx与服务器使用Socket进行通信的框架,还不太熟悉的请移步到如下博文中: [C/S通信交互之Socket篇]Cocos2dx(Client)使用BSD Socke ...

  9. delphi备份恢复剪切板(使用了GlobalLock API函数和CopyMemory)

    看了季世平老兄的C++代码后翻译过来的 unit clipbak; interface uses SysUtils, Classes, Clipbrd, Windows, Contnrs; type ...

  10. 关于Android 7.0更新后调用系统相机及电筒问题

    android升级到7.0后对权限又做了一个更新即不允许出现以file://的形式调用隐式APP,需要用共享文件的形式:content:// URI 因为系统相机是提供的共享 Provider , C ...