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];
        }
    }
  • 相关阅读:
    Blazor使用Chrome远程调试
    Blazor登录Ids4
    Jenkins + Coding 构建 Docker Image 并自动上传至Docker Registry
    EFK 数据生命周期
    EFK (Elasticsearch + Fluentd + Kibana) 日志分析系统
    通过Nginx代理Grafana,并通过域名访问
    Prometheus搜集mysql和nginx log指标
    java 线程相关(4)
    java 并发相关(5)
    java 线程相关(3)
  • 原文地址:https://www.cnblogs.com/edensyd/p/9198278.html
Copyright © 2011-2022 走看看