本文介绍了在Swift中压缩GIF的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我的gif生成器功能降低了分辨率并使一系列照片中的帧最小化,但是我没有什么可以减少颜色数量或使其有损的.我将如何在Swift 4中做到这一点?

My gif generator function reduces the resolution and minimizes the frames from a series of photos, but I don't have anything to reduce the amount of colors or make it lossy. How would I go about doing this in 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兆字节的文件.我想它可能会小得多.

Anything above 500 width or height returns a file that is around 11 megabytes. I imagine it could be far smaller.

推荐答案

在gifsicle 签出 giflossy /kornelski/giflossy"rel =" nofollow noreferrer> github .它确实可以满足您的需求,而且功能更多.它在C语言中,但您可以从中获得一些启发,并迅速移植相关部分.

Check out giflossy based ongifsicle on github. It does exactly what you want and more. It’s in C but you can get some inspiration from there and port the relevant parts to swift.

这篇关于在Swift中压缩GIF的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:08