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

来源:互联网 发布:网络推广都做些什么 编辑:程序博客网 时间:2024/04/30 09:40

之前做过一个关于gif的app,一直没有来得及记录,现在记录一下学习的过程.

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


gif分解图片


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


gif的制作

制作gif需要依赖MobileCoreServices.framework


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


源码:GIF制作


参考文章:iOS:GIF图片的预览以及生成 iOS由ImageIO.framework实现gif的系统解码 - VicStudio CGImageSource对图像数据读取任务的抽象

0 0
原创粉丝点击