zoukankan      html  css  js  c++  java
  • iOS_UIImage_Gif的合成

    /**
     1. 数据获取
     2. 创建Gif文件
     3. 配置Gif属性
     4. 单帧添加到gif
     */

    github地址: https://github.com/mancongiOS/UIImage.git

    导入: 

    #import <ImageIO/ImageIO.h>
    #import <MobileCoreServices/MobileCoreServices.h>

    实现: 

        // 1. 获取数据
        NSMutableArray * imageArrayM = [NSMutableArray arrayWithCapacity:0];
        [imageArrayM addObject:[UIImage imageNamed:@"1.png"]];
        [imageArrayM addObject:[UIImage imageNamed:@"2"]];
        [imageArrayM addObject:[UIImage imageNamed:@"3"]];
        [imageArrayM addObject:[UIImage imageNamed:@"4"]];
        [imageArrayM addObject:[UIImage imageNamed:@"5"]];
        [imageArrayM addObject:[UIImage imageNamed:@"6"]];
        [imageArrayM addObject:[UIImage imageNamed:@"7"]];
        [imageArrayM addObject:[UIImage imageNamed:@"8"]];
    
        // 2. 创建Gif文件
        NSArray * document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString * documentStr = [document objectAtIndex:0];
        NSFileManager * fileManager = [NSFileManager defaultManager];
        NSString * textDic = [documentStr stringByAppendingString:@"/gif"];
        [fileManager createDirectoryAtPath:textDic withIntermediateDirectories:YES attributes:nil error:nil];
        NSString * path = [textDic stringByAppendingString:@"test1.gif"];
        NSLog(@"path: %@",path);
        
        // 3. 配置gif属性
        CGImageDestinationRef destion;
        // 将path映射成url对象
        CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false);
        destion = CGImageDestinationCreateWithURL(url, kUTTypeGIF, imageArrayM.count, NULL);
    
        NSMutableDictionary * dictM = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.3],(NSString *)kCGImagePropertyGIFDelayTime, nil];
        NSDictionary * frameDic = [NSDictionary dictionaryWithObject:dictM forKey:(NSString *)kCGImagePropertyGIFDelayTime];
        
        NSMutableDictionary * gifParaDict = [NSMutableDictionary dictionaryWithCapacity:2];
        
        // 设置颜色
        [gifParaDict setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap];
        
        // 设置模式
        [gifParaDict setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];
        
        // 设置颜色深度
        [gifParaDict setObject:[NSNumber numberWithInt:8] forKey:(NSString *)kCGImagePropertyDepth];
        
        // 是否可以重复播放
        [gifParaDict setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
        
        NSDictionary * gifProperty = [NSDictionary dictionaryWithObject:gifParaDict forKey:(NSString *)kCGImagePropertyGIFDictionary];
        
        // 单帧添加到gif
        for (UIImage * dImage in imageArrayM) {
            CGImageDestinationAddImage(destion, dImage.CGImage, (__bridge CFDictionaryRef)frameDic);
        }
        
        CGImageDestinationSetProperties(destion, (__bridge CFDictionaryRef)gifProperty);
        CGImageDestinationFinalize(destion);
        CFRelease(url);
        CFRelease(destion);
  • 相关阅读:
    JS 页面生成锚点
    JavaScript AMD模块化规范
    Canvas文字的渲染基础 Better
    Canvas曲线绘制 Better
    clickhouse配置登录密码
    git pull 拉取报错:fatal: refusing to merge unrelated histories
    JAVA根据A星算法规划起点到终点二维坐标的最短路径
    JAVA使用netty建立websocket连接
    CSS设置文字超出部分自动换行
    SpringBoot(SpringMVC)使用addViewControllers设置统一请求URL重定向(映射、转发)配置
  • 原文地址:https://www.cnblogs.com/mancong/p/6138172.html
Copyright © 2011-2022 走看看