• iOS_UIImage_Gif的分解


    /** Gif的步骤
     1. 拿到Gifd的数据
     2. 将Gif分解为一帧帧
     3. 将单帧数据转为UIImage
     4. 单帧图片保存
     */

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

    导入:

    #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];
        }
    }
  • 相关阅读:
    Vim配置及使用技巧
    终端提示符的配置
    Archlinux下i3wm与urxvt的配置
    Linux压缩命令
    Archlinux无线联网教程
    Archlinux安装和使用技巧
    Linux下硬盘分区
    Linux挂载
    Android中pullToRefresh使用
    SVN服务器搭建和使用教程
  • 原文地址:https://www.cnblogs.com/mancong/p/6138168.html
Copyright © 2020-2023  润新知