美文网首页
ios生成GIF图

ios生成GIF图

作者: Arthur澪 | 来源:发表于2018-02-06 10:41 被阅读0次

自定义方法

-(void)creatGif1{
    //  图片 数组
    NSMutableArray * images= [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"动画1"],[UIImage imageNamed:@"动画2"],[UIImage imageNamed:@"动画3"],[UIImage imageNamed:@"动画4"], nil];
    
    //创建gif文件
    NSArray *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *doucmentStr =[document objectAtIndex:0];
    
    NSFileManager *filemanager = [NSFileManager defaultManager];
    NSString *textDic = [doucmentStr stringByAppendingString:@"/gif"];  //文件夹
    [filemanager createDirectoryAtPath:textDic withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *path = [textDic stringByAppendingString:@"test1.gif"];
    NSLog(@"-----%@",path);   // GIF 的保存路径
    
    //配置gif属性
    CGImageDestinationRef destion;
    //    路径url,图片类型,图片数,
    CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false);
    destion = CGImageDestinationCreateWithURL(url, kUTTypeGIF, images.count, NULL);
    
    //帧 时间间隔
    NSDictionary *frameDic = [NSDictionary dictionaryWithObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:5.0],(NSString*)kCGImagePropertyGIFDelayTime, nil] forKey:(NSString*)kCGImagePropertyGIFDelayTime];
    
    //git参数:颜色空间,颜色模式,深度,循环次数
    NSMutableDictionary *gifParmdict = [NSMutableDictionary dictionaryWithCapacity:2];
    [gifParmdict setObject:[NSNumber numberWithBool:YES] forKey:(NSString*)kCGImagePropertyGIFHasGlobalColorMap];
    [gifParmdict setObject:(NSString*)kCGImagePropertyColorModelRGB forKey:(NSString*)kCGImagePropertyColorModel];
    [gifParmdict setObject:[NSNumber numberWithInt:8] forKey:(NSString*)kCGImagePropertyDepth];
    [gifParmdict setObject:[NSNumber numberWithInt:0] forKey:(NSString*)kCGImagePropertyGIFLoopCount];
    NSDictionary *gifProperty = [NSDictionary dictionaryWithObject:gifParmdict forKey:(NSString*)kCGImagePropertyGIFDictionary];
    
    for (UIImage *dimage in images) {
        // 遍历图片,附带GIF参数,生成GIF图
        CGImageDestinationAddImage(destion, dimage.CGImage, (__bridge CFDictionaryRef)frameDic);
    }
    // 添加GIF属性
    CGImageDestinationSetProperties(destion,(__bridge CFDictionaryRef)gifProperty);
    CGImageDestinationFinalize(destion);   // 最终确定 生成
    CFRelease(destion);  // 释放资源   
}

第三方库

YYImage
https://github.com/ibireme/YYImage

相关文章

网友评论

      本文标题:ios生成GIF图

      本文链接:https://www.haomeiwen.com/subject/arofoxtx.html