IOS GIF图片的解析
来源:互联网 发布:淘宝男鞋拍照视频教程 编辑:程序博客网 时间:2024/05/18 02:16
- //加载gif
- 02
- 03 NSString *filePath = [[NSBundle mainBundle]pathForResource:@"bai3" ofType:@"gif"];
- 04
- 05 NSData *data = [NSData dataWithContentsOfFile:filePath];
- 06
- 07 CGImageSourceRef gif = CGImageSourceCreateWithData((CFDataRef)data, nil);
- 08
- 09 //获取gif的各种属性
- 10
- 11 CFDictionaryRef gifprops =(CGImageSourceCopyPropertiesAtIndex(gif,0,NULL));
- 12
- 13 NSLog(@"_______%@",gifprops);
- 14
- 15
- 16 NSInteger count =CGImageSourceGetCount(gif);
- 17
- 18 NSLog(@"________%d",count);
- 19
- 20
- 21 CFDictionaryRef gifDic = CFDictionaryGetValue(gifprops, kCGImagePropertyGIFDictionary);
- 22
- 23 CFDictionaryRef delay = CFDictionaryGetValue(gifDic, kCGImagePropertyGIFDelayTime);
- 24
- 25 NSLog(@"_______%@",delay);
- 26
- 27
- 28 //[gifDic objectForKey:(NSString *)kCGImagePropertyGIFDelayTime];
- 29
- 30 // NSNumber * w = CFDictionaryGetValue(gifprops, @"PixelWidth");
- 31
- 32 // NSNumber * h =CFDictionaryGetValue(gifprops, @"PixelHeight");
- 33
- 34 // float totalDuration = delay.doubleValue * count;
- 35
- 36 // float pixelWidth = w.intValue;
- 37
- 38 // float pixelHeight = h.intValue;
- 39
- 40 //将gif解析成UIImage类型对象,并加进images数组中
- 41
- 42
- 43 NSMutableArray *images = [NSMutableArray arrayWithCapacity:count];
- 44
- 45 for(int index=0;index<count;index++)
- 46
- 47 {
- 48
- 49 CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, nil);
- 50
- 51 UIImage *img = [UIImage imageWithCGImage:ref];
- 52
- 53 [images addObject:img];
- 54
- 55 CFRelease(ref);
- 56
- 57 }
- 58
- 59 CFRelease(gifprops);
- 60
- 61 CFRelease(gif);
Gif的合成
- - (void)exportAnimatedGif:(CGImageSourceRef )gif :(NSMutableArray *)images
- 02
- 03 {
- 04
- 05 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"animated.gif"];
- 06
- 07 CGImageDestinationRef destination = CGImageDestinationCreateWithURL(( CFURLRef)[NSURL fileURLWithPath:path],
- 08
- 09 kUTTypeGIF,
- 10
- 11 images.count,
- 12
- 13 NULL);
- 14
- 15 UIImage *image;
- 16
- 17 for (int i = 0; i<images.count; i++)
- 18
- 19 {
- 20
- 21 image = images[i];
- 22
- 23 CFDictionaryRef gifprops =(CGImageSourceCopyPropertiesAtIndex(gif,i,NULL));
- 24
- 25 CFDictionaryRef gifDic = CFDictionaryGetValue(gifprops, kCGImagePropertyGIFDictionary);
- 26
- 27 NSNumber *delay = CFDictionaryGetValue(gifDic, kCGImagePropertyGIFDelayTime);
- 28
- 29 NSDictionary *gifDelay = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:delay forKey:(NSString *)kCGImagePropertyGIFDelayTime]
- 30
- 31 forKey:(NSString *)kCGImagePropertyGIFDictionary];
- 32
- 33
- 34
- 35 CGImageDestinationAddImage(destination,image.CGImage, (CFDictionaryRef)gifDelay);
- 36
- 37 CGImageDestinationSetProperties(destination, ( CFDictionaryRef)gifprops);
- 38
- 39 }
- 40
- 41
- 42
- 43 // CGImageDestinationSetProperties(destination, ( CFDictionaryRef)gifprops);
- 44
- 45 CGImageDestinationFinalize(destination);
- 46
- 47 CFRelease(destination);
- 48
- 49 NSLog(@"animated GIF file created at %@", path);
- 50
- 51
- 52 }
24.将一个UIView对象的内容保存为UIImage
- + (UIImage*)imageFromView:(UIView*)view{
- 02
- 03 UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, view.layer.contentsScale);
- 04
- 05 [view.layer renderInContext:UIGraphicsGetCurrentContext()];
- 06
- 07 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- 08
- 09 UIGraphicsEndImageContext();
- 10
- 11 return image;
- 12
- 13 }
注意:生成的图片的scale和view的scale一致,这样才可以保证图片的效果和view显示的完全一致,使用renderInContext方法可以让subviews的内容也显示的图片里。
- IOS GIF图片的解析
- Gif图片的解析
- iOS中GIF图片的解析+合成
- gif格式图片的解析
- IOS gif图片的显示
- IOS GIF图片解析 将gif图片解析成image数组
- IOS GIF图片解析 将gif图片解析成image数组
- iOS 中 GIF 图片的显示方法
- iOS:GIF图片的预览以及生成
- iOS:GIF图片的预览以及生成
- iOS基础GIF图片的实现
- iOS中GIF图片的加载
- iOS中gif图片上传的问题
- iOS gif图片的PHAsset转nsdata
- IOS显示GIF图片
- ios播放gif图片
- ios加载gif图片
- ios播放gif图片
- HADOOP的学习笔记 (第四期) eclipse 执行 wordcount .
- 数据结构与算法B作业一,概论
- [转]阿里云CentOS Linux服务器上用postfix搭建邮件服务器
- HADOOP的学习笔记 (第五期) hadoop示例代码分析 .
- javascript原型继承分析(prototype)(补充版)
- IOS GIF图片的解析
- Java是静态语言也是强类型定义语言
- 网站建设系列之关键词的设置
- LeetCode | Longest Consecutive Sequence
- [各种面试题] 复制有向图
- HDU 4115 Eliminate the Conflict (2-sat,4级)
- OC-NSString字符串导出(写入到文件)
- hdu 1401 Solitaire 双向bfs
- JQuery小结