iOS Gif图片压缩

作者: PencilCarefully | 来源:发表于2019-02-28 14:46 被阅读12次

    随着表情包越来越流行,以后项目中使用到Gif图片的地方也越来越多,遇到很大的gif图片的时候,这时候移动端要首先做一步gif图片的压缩操作,再上传到服务器,就跟上传图片的时候大多是要先压缩图片一样.我做了一个swift版本的压缩图片的demo,在文章最后面会给出下载链接.废话不多说,先来效果图.

    压缩gif图片.gif

    压缩思路分析

    • 压缩gif图片不能像直接压缩图片的方式一样压缩, 使用 UIImageJPEGRepresentation(image!,0.5) 直接压缩的话,只能取出gif图片的首帧图.所以想要压缩gif图片,大致有以下思路做.

    • 1 获取gif图的每一帧图片

    • 2 对每一帧图片进行压缩

    • 3 对gif图进行抽帧处理

    具体代码实现

    1. 获取gif的每一帧图片,这个需要用到iOS的ImageIO框架.
     guard let imageSource = CGImageSourceCreateWithData(rawData as CFData, [kCGImageSourceShouldCache: false] as CFDictionary),
                let writeData = CFDataCreateMutable(nil, 0),
                let imageType = CGImageSourceGetType(imageSource) else {
                    return nil
            }
    
    1. 计算帧的间隔,合并帧的时间,做抽帧处理,抽取 每n帧,使用一帧, 具体是看gif图的帧数大小而定的.
           // 计算帧的间隔
            let frameDurations = imageSource.frameDurations
            
            // 合并帧的时间,最长不可高于 200ms
            let mergeFrameDurations = (0..<frameDurations.count).filter{ $0 % sampleCount == 0 }.map{ min(frameDurations[$0..<min($0 + sampleCount, frameDurations.count)].reduce(0.0) { $0 + $1 }, 0.2) }
            
            // 抽取帧 每 n 帧使用 1 帧
            let sampleImageFrames = (0..<frameDurations.count).filter{ $0 % sampleCount == 0 }.compactMap{ CGImageSourceCreateImageAtIndex(imageSource, $0, nil) }
            
            guard let imageDestination = CGImageDestinationCreateWithData(writeData, imageType, sampleImageFrames.count, nil) else{
                return nil
            }
    
    1. 对每一帧进行重新编码
            // 每一帧图片都进行重新编码
            zip(sampleImageFrames, mergeFrameDurations).forEach{
                // 设置帧间隔
                let frameProperties = [kCGImagePropertyGIFDictionary : [kCGImagePropertyGIFDelayTime: $1, kCGImagePropertyGIFUnclampedDelayTime: $1]]
                CGImageDestinationAddImage(imageDestination, $0, frameProperties as CFDictionary)
            }
            
    
    1. 判断压缩之后的大小,是否小于自己所需要的大小,如果比自己想要的大,进行尺寸压缩.
    
     let options = [kCGImageSourceThumbnailMaxPixelSize: limitLongWidth, kCGImageSourceCreateThumbnailWithTransform:true, kCGImageSourceCreateThumbnailFromImageIfAbsent:true] as CFDictionary
            
            if frameCount > 1 {
                // 计算帧的间隔
                let frameDurations = imageSource.frameDurations
                
                // 每一帧都进行缩放
                let resizedImageFrames = (0..<frameCount).compactMap{ CGImageSourceCreateThumbnailAtIndex(imageSource, $0, options) }
                
                // 每一帧都进行重新编码
                zip(resizedImageFrames, frameDurations).forEach {
                    // 设置帧间隔
                    let frameProperties = [kCGImagePropertyGIFDictionary : [kCGImagePropertyGIFDelayTime: $1, kCGImagePropertyGIFUnclampedDelayTime: $1]]
                    CGImageDestinationAddImage(imageDestination, $0, frameProperties as CFDictionary)
                }
            }
    
    经过以上的步骤,就可以把gif图片压缩到我们想要的大小.当然压缩也不是无限制的,一个很大的gif图片,压缩的太小的话,抽出的帧数就会很多,播放起来就不是特别的连贯.
    关于gif图的播放, 使用SDWebImage就很容易实现了.

    gifImageView.image = UIImage.sd_image(withGIFData: data as Data?)

    • -- 小工具 字节数转KB,MB , NSData的分类,复制粘贴的别人代码,OC的.
    - (NSString *)transformedValue{
        
        double convertedValue = [[NSNumber numberWithInteger:self.length] doubleValue];
        
        int multiplyFactor = 0;
        
        NSArray *tokens = [NSArray arrayWithObjects:@"bytes",@"KB",@"MB",@"GB",@"TB",@"PB", @"EB", @"ZB", @"YB",nil];
        
        while (convertedValue > 1024) {
            
            convertedValue /= 1024;
            
            multiplyFactor++;
        }
        
        return [NSString stringWithFormat:@"%4.2f %@",convertedValue, [tokens objectAtIndex:multiplyFactor]];
    }
    
    

    以上就是gif压缩的整体思路流程了,附上github地址.希望能对需要的人有一点帮助吧.

    相关文章

      网友评论

        本文标题:iOS Gif图片压缩

        本文链接:https://www.haomeiwen.com/subject/zakzyqtx.html