zoukankan      html  css  js  c++  java
  • 分解gif图片并保存

    /** Gif的步骤
     1. 拿到Gifd的数据
     2. 将Gif分解为一帧帧
     3. 将单帧数据转为UIImage
     4. 单帧图片保存
     */
    #import <ImageIO/ImageIO.h>     // 图像的输入输出文件
    #import <MobileCoreServices/MobileCoreServices.h>
    - (void)didCompositionGif {
    
        //1. 拿到gif数据
        NSString * gifPathSource = [[NSBundle mainBundle] pathForResource:@"kobe" ofType:@"gif"];
        NSData * data = [NSData dataWithContentsOfFile:gifPathSource];
    #warning 桥接的意义 (__bridge CFDataRef)
    
        CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
        
        //2. 将gif分解为一帧帧
        size_t count = CGImageSourceGetCount(source);
        NSLog(@"%zu",count);
        
        NSMutableArray * tmpArray = [NSMutableArray arrayWithCapacity:0];
        for (int i = 0; i < count; i ++) {
            CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL);
            
            //3. 将单帧数据转为UIImage
            UIImage * image = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
            [tmpArray addObject:image];
    #warning CG类型的对象 不能用ARC自动释放内存.需要手动释放
            CGImageRelease(imageRef);
        }
        CFRelease(source);
        
        // 单帧图片保存
        int i = 0;
        for (UIImage * image  in tmpArray) {
            
            i ++;
            NSData * data = UIImagePNGRepresentation(image);
            NSArray * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            
            NSString * gifPath = path[0];
            NSLog(@"gifPath: %@",gifPath);
            NSString * pathNum = [gifPath stringByAppendingString:[NSString stringWithFormat:@"%d.png",i]];
            [data writeToFile:pathNum atomically:NO];
        }
    }
  • 相关阅读:
    activiti5.13 框架 数据库表结构说明
    c3p0详细配置
    linux+nginx+tomcat负载均衡,实现session同步
    Lvs+Keepalived+MySQL Cluster架设高可用负载均衡Mysql集群
    java jstack dump 线程 介绍 解释
    JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解
    CheckStyle使用手册(一)
    checkstyle使用介绍
    memcache启动多个服务
    temp
  • 原文地址:https://www.cnblogs.com/edensyd/p/9198278.html
Copyright © 2011-2022 走看看