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];
        }
    }
  • 相关阅读:
    ping与telnet的区别
    TCP连接的建立与关闭
    网络的7层协议
    oracle数据库中,分天查询数目
    求某个字符在字符串中的第5个位置
    高精度乘
    高精度加法
    二叉排序树(建树,先序,中序,后序遍历)
    求哈夫曼树的带权路径长度和
    HDU_1237_简单计算器
  • 原文地址:https://www.cnblogs.com/edensyd/p/9198278.html
Copyright © 2011-2022 走看看