iOS中GIF的制作的简单版本记录

http://www.2cto.com/kf/201312/268731.html

制作gif是基于ImageIO.framework,所以要先添加这个库

 

gif分解图片

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
- ( void )viewDidLoad
{
     [ super viewDidLoad];
 
     NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource: @test101 ofType: @gif ]];
 
     //通过data获取image的数据源
     CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
     
     //获取帧数
     size_t count = CGImageSourceGetCount(source);
     
     NSMutableArray* tmpArray = [NSMutableArray array];
     for (size_t i = 0 ; i < count; i++)
     {
         //获取图像
         CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL);
         
         //生成image
         UIImage *image = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
         
         [tmpArray addObject:image];
         
         CGImageRelease(imageRef);
     }
     CFRelease(source);
     
     int i = 0 ;
     for (UIImage *img in tmpArray) {
         //写文件
         NSData *imageData = UIImagePNGRepresentation(img);
         
         NSString *pathNum = [[self backPath] stringByAppendingPathComponent:[NSString stringWithFormat:@%d.png,i]];
         [imageData writeToFile:pathNum atomically:NO];
         i++;
     }
        
}
 
//返回保存图片的路径
-(NSString *)backPath{
     NSFileManager *fileManager = [NSFileManager defaultManager];
     
     NSString *path = [UniversalMethod backDocumentDirectoryPath];
     
     NSString *imageDirectory = [path stringByAppendingPathComponent: @Normal ];
     
     [fileManager createDirectoryAtPath:imageDirectory withIntermediateDirectories:YES attributes:nil error:nil];
     return imageDirectory;
}

 

gif的制作

制作gif需要依赖MobileCoreServices.framework

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//gif的制作
     
     //获取源数据image
     NSMutableArray *imgs = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed: @bear_1 ],[UIImage imageNamed: @bear_2 ], nil];
     
     //图像目标
     CGImageDestinationRef destination;
     
     //创建输出路径
     NSArray *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentStr = [document objectAtIndex: 0 ];
     NSFileManager *fileManager = [NSFileManager defaultManager];
     NSString *textDirectory = [documentStr stringByAppendingPathComponent: @gif ];
     [fileManager createDirectoryAtPath:textDirectory withIntermediateDirectories:YES attributes:nil error:nil];
     NSString *path = [textDirectory stringByAppendingPathComponent: @test101 .gif];
     
     
     NSLog(@%@,path);
     
     //创建CFURL对象
     /*
      CFURLCreateWithFileSystemPath(CFAllocatorRef allocator, CFStringRef filePath, CFURLPathStyle pathStyle, Boolean isDirectory)
      
      allocator : 分配器,通常使用kCFAllocatorDefault
      filePath : 路径
      pathStyle : 路径风格,我们就填写kCFURLPOSIXPathStyle 更多请打问号自己进去帮助看
      isDirectory : 一个布尔值,用于指定是否filePath被当作一个目录路径解决时相对路径组件
      */
     CFURLRef url = CFURLCreateWithFileSystemPath (
                                                   kCFAllocatorDefault,
                                                   (CFStringRef)path,
                                                   kCFURLPOSIXPathStyle,
                                                   false );
     
     //通过一个url返回图像目标
     destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, imgs.count, NULL);
     
     //设置gif的信息,播放间隔时间,基本数据,和delay时间
     NSDictionary *frameProperties = [NSDictionary
                                      dictionaryWithObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat: 0.3 ], (NSString *)kCGImagePropertyGIFDelayTime, nil]
                                      forKey:(NSString *)kCGImagePropertyGIFDictionary];
     
     //设置gif信息
     NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity: 2 ];
     
     [dict setObject:[NSNumber numberWithBool:YES] forKey:(NSString*)kCGImagePropertyGIFHasGlobalColorMap];
     
     [dict setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];
     
     [dict setObject:[NSNumber numberWithInt: 8 ] forKey:(NSString*)kCGImagePropertyDepth];
     
     [dict setObject:[NSNumber numberWithInt: 0 ] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
     NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:dict
                                                               forKey:(NSString *)kCGImagePropertyGIFDictionary];
     //合成gif
     for (UIImage* dImg in imgs)
     {
         CGImageDestinationAddImage(destination, dImg.CGImage, (__bridge CFDictionaryRef)frameProperties);
     }
     CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)gifProperties);
     CGImageDestinationFinalize(destination);
     CFRelease(destination);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值