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);
  • 相关阅读:
    概率算法实现八皇后问题-cpp
    交互式多媒体图书平台的设计
    Map容器
    序列容器和容器适配器
    ubuntu18.04 基于VSCode的C++环境搭建
    工程化编程实战callback接口学习
    stl_string
    通过filebeat的modules搜集nginx日志
    gitlab-runner安装配置
    EFK搜集MySQL慢日志
  • 原文地址:https://www.cnblogs.com/mancong/p/6138172.html
Copyright © 2011-2022 走看看