首页 > 解决方案 > 使用 Swift/Objective-C 减少色带以从最多 200 个视频帧创建 GIF

问题描述

我想从视频帧制作 GIF。每帧(UIImage)大小最多为960 * 540。由于 GIF 绑定到 256 种颜色,我遇到了更多颜色问题。帧生成不佳。经过这么多研究,我发现了可以减少色带的抖动方法。我没有采用这种方法,因为抖动的默认时间复杂度是 O(N^2),其中 N 是图像宽度和高度的乘积。我不知道如何在不抖动 200 帧的情况下有效地减少色带。是否有任何其他可用的技术或过滤器可以有效地减少色带? 原始视频:原始视频 我制作的 GIF: 的 GIF 我需要的 GIF:better-GIF




我的方法:首先,我将所有帧保存为来自 Video 的 UIImage。然后我使用以下代码创建了 GIF:

func generateImagesToGif(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.5]]
        let cfURL = URL(fileURLWithPath: path) as CFURL
        if let destination = CGImageDestinationCreateWithURL(cfURL, kUTTypeGIF, photos.count, nil) {
            CGImageDestinationSetProperties(destination, fileProperties as CFDictionary?)
            for photo in photos {
                CGImageDestinationAddImage(destination, photo.cgImage!, gifProperties as CFDictionary?)
            }
            return CGImageDestinationFinalize(destination)
        }
        return false
    }

更新:看看这个(高质量 GIF)。他们的 GIF 看起来像原始视频。非常低的色带!

标签: iosswiftobjective-cgif256color

解决方案


推荐阅读