在Swift中压缩GIF的方法?

时间:2018-01-29 19:10:29

标签: swift compression animated-gif lossy-compression

我的gif生成器功能降低了分辨率,并最大限度地减少了一系列照片中的帧数,但我没有任何东西可以减少颜色数量或使其有损。我将如何在Swift 4中执行此操作?

    func generateGif(photos: [UIImage], filename: String) -> Bool {

        let documentsDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let path = documentsDirectoryPath.appending(filename)
        let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]]
        let gifProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: 0.1]] // actual speed is 0.2
        let cfURL = URL(fileURLWithPath: path) as CFURL
        if let destination = CGImageDestinationCreateWithURL(cfURL, kUTTypeGIF, photos.count, nil) {

            CGImageDestinationSetProperties(destination, fileProperties as CFDictionary?)

            var inc:Int = 0

            for _ in 0..<15 { // actual photos.count is 31

                let photo = photos[inc]

                var width:CGFloat = photo.size.width
                var height:CGFloat = photo.size.height

                let maxWidthHeight:CGFloat = 500

                if(photo.size.width > maxWidthHeight || photo.size.height > maxWidthHeight){ // gif beyond max width or height

                    if(photo.size.width > photo.size.height){

                        width = maxWidthHeight

                        let percentChange = width / photo.size.width
                        height = photo.size.height * percentChange

                    }else{

                        height = maxWidthHeight

                        let percentChange = height / photo.size.height
                        width = photo.size.width * percentChange

                    }

                    print("w:\(width) h:\(height) inc:\(inc)")

                }

                let smallerPhoto = imageWithImage(image:photo, scaledToSize:CGSize(width:width, height:height))
                CGImageDestinationAddImage(destination, smallerPhoto.cgImage!, gifProperties as CFDictionary?)

                inc+=2 // skipping ahead incrementation for less frames
            }

// here it's saving to a photo album when it's done and reporting the filesize
            CustomPhotoAlbum.sharedInstance.savePhoto(URL: URL(fileURLWithPath: path), completion: {

                var fileSize : UInt64

                do {
                    let attr = try FileManager.default.attributesOfItem(atPath: path)
                    fileSize = attr[FileAttributeKey.size] as! UInt64
                    print("gif is \(fileSize/1000000) megabytes")
                } catch {
                    print("Error: \(error)")
                }

            })

            return CGImageDestinationFinalize(destination)
        }


        return false
    }

高于500宽度或高度的任何内容都会返回大约11兆字节的文件。我想它可能要小得多。

1 个答案:

答案 0 :(得分:1)

根据github上的giflossy查看gifsicle。它完全符合您的要求和更多。它在C中,但你可以从那里获得一些灵感并将相关部分移植到swift。