分解gif图片并保存

来源:互联网 发布:淘宝网新款凉鞋罗马鞋 编辑:程序博客网 时间:2024/04/28 11:21

代码如下:

-(void)deCompositionGif{
    NSString *gitpathSource = [[NSBundle mainBundle]pathForResource:@"名称" ofType:@"gif"];
    NSData *data =[NSData dataWithContentsOfFile:gitpathSource];
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    //将gif分解成一帧帧
    size_t count = CGImageSourceGetCount(source);
    NSMutableArray *tmpArray =[[NSMutableArray alloc]init];
    for (size_t i = 0; i < count; i++) {
     CGImageRef imageref  =  CGImageSourceCreateImageAtIndex(source, 1, NULL);
        //将单帧数据转化成Image
        UIImage *image = [UIImage imageWithCGImage:imageref scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
        [tmpArray addObject:image];
        CGImageRelease(imageref);
    }
    CFRelease(source);

//前面两处释放是为了防止内存泄漏

    int i = 0;
    for (UIImage *image in tmpArray) {
        NSData *data = UIImagePNGRepresentation(image);
        NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *gifPath = path[0];
        NSString *pathNum = [gifPath stringByAppendingString:[NSString stringWithFormat:@"%d.png",i]];
        i++;
        [data writeToFile:pathNum atomically:NO];
    }
    
}



 

OC引入#import <ImageIO/ImageIO.h>

swift引入import ImageIO


swift代码:

 func deCompositionGif() {
        let gitpathSource = Bundle.main.path(forResource: "1", ofType: "gif")
        let data = NSData.init(contentsOfFile: gitpathSource!)
        
        let source = CGImageSourceCreateWithData(data!, nil)
            //将gif分解成一帧帧
        var count = CGImageSourceGetCount(source!)
        let tmpArray : NSMutableArray = []
        while count > 0 {
            let imageref = CGImageSourceCreateImageAtIndex(source!, 1, nil)

            let  image = UIImage.init(cgImage: imageref!, scale: UIScreen.main.scale, orientation: UIImageOrientation.up)
            count -= 1;
            tmpArray.add(image)
        }

        var i = 0
        
        for image in tmpArray {
            let data : NSData = UIImagePNGRepresentation(image as! UIImage)! as NSData
            let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0]
            let pathNum = path + "\(i).png";
                i += 1;
            data.write(toFile: pathNum, atomically: true);
            UIImageWriteToSavedPhotosAlbum(image as! UIImage, nil, nil, nil)
            
            }
            
      
    }

最重要的一点是比忘了添加 <key>NSPhotoLibraryUsageDescription</key> <string>App需要您的同意,才能访问相册</string>  iOS 10如果访问必须添加否则闪退

在OC代码中没有进行保存到手机相册中,如果需要自己写入就好

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 a1驾照时期过了怎么办 b1驾照扣了12分怎么办 北京的驾照换证怎么办 b2汽车驾驶证年审过期几天怎么办 上海驾照到期人在外地怎么办 交警开的罚单交不了怎么办 珠海交警微信交罚单扣分怎么办 驾驶证违法罚款单子没有了怎么办 转账密码输错3次怎么办 汽车违章扣6分怎么办 汽车扣了72分年检怎么办 汽车扣了50分怎么办 汽车扣了15分怎么办 汽车扣了27分怎么办 汽车扣了40分怎么办 行驶证掉了怎么办 异地 高速上没带驾驶证行驶证怎么办 身份证驾驶证行驶证都丢了怎么办 驾驶证年审过期一个月怎么办 柴油车辆年检尾气不合格怎么办 驾驶证过期了5天怎么办 过了审车时间怎么办 骑车没带行驶证怎么办 轿车行驶证丢了怎么办 车子没年检被扣怎么办 上海车辆年检过期了怎么办 行驶证过期十天怎么办 行驶证盖章满了怎么办 驾照c证扣12分怎么办 两年小车忘年审怎么办 4年车检过期了怎么办 车检过期了1周怎么办 超过检车几天了怎么办 驾驶证扣两个6分怎么办 行驶证检验有效期过期怎么办 驾驶证被扣26分怎么办 c1驾驶证扣26分怎么办 驾驶证被扣6分后怎么办 c1驾照年审过期一天怎么办 审驾照时间过了怎么办 b2驾驶证扣了分怎么办