在iOS 11中创建Gif图像颜色映射 [英] Creating Gif Image Color Maps in iOS 11

查看:156
本文介绍了在iOS 11中创建Gif图像颜色映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在创建Gif时遇到问题,如果Gif太大,颜色就会丢失。但是,感谢SO的帮助,有人能够帮助我找到解决方法并创建我自己的颜色图。

I recently was having an issue when creating a Gif where if it got too big colors went missing. However thanks to help from SO someone was able to help me find a work around and create my own color map.

此处的上一个问题... 保存动画Gif时,iOS颜色不正确

Previous Question here...iOS Colors Incorrect When Saving Animated Gif

这在iOS 11之前一直有效。我在文档中找不到任何更改的内容,这将使其不再起作用。我发现的是,如果我删除 kCGImagePropertyGIFImageColorMap 会生成一个gif,但是它的原始问题是,如果gif像我以前的问题一样变大,颜色就会丢失。

This worked great up until iOS 11. I can't find anything in the docs that changed and would make this no longer work. What I have found is if I remove kCGImagePropertyGIFImageColorMap A gif is generated but it has the original issue where colors go missing if the gif gets to large like my previous question. This makes sense as this was added to fix that issue.

可疑问题...

func createGifFromImage(_ image: UIImage) -> URL{

    let fileProperties: [CFString: CFDictionary] = [kCGImagePropertyGIFDictionary: [
        kCGImagePropertyGIFHasGlobalColorMap: false as NSNumber] as CFDictionary]

    let documentsDirectoryPath = "file://\(NSTemporaryDirectory())"

    if let documentsDirectoryURL = URL(string: documentsDirectoryPath){

        let fileURL = documentsDirectoryURL.appendingPathComponent("test.gif")
        let destination = CGImageDestinationCreateWithURL(fileURL as CFURL, kUTTypeGIF, 1, nil)!

        CGImageDestinationSetProperties(destination, fileProperties as CFDictionary);

        let colorMap = image.getColorMap()
        print("ColorMap \(colorMap.exported as NSData)")

        let frameProperties: [String: AnyObject] = [
            String(kCGImagePropertyGIFImageColorMap): colorMap.exported as NSData
        ]

        let properties: [String: AnyObject] = [
            String(kCGImagePropertyGIFDictionary): frameProperties as AnyObject
        ]

        CGImageDestinationAddImage(destination, image.cgImage!, properties as CFDictionary);

        if (!CGImageDestinationFinalize(destination)) {
            print("failed to finalize image destination")
        }

        return fileURL
    }

    //shouldn't get here
    return URL(string: "")!
}

此处是下载测试项目的链接。请注意,如果您在10.3模拟器上运行它,效果很好,但如果在iOS 11上运行,则它是白色图像。

Here is a link to download a test project. Note if you run it on a 10.3 simulator it works great but if you run it on iOS 11 it is a white image.

https://www.dropbox.com/s/hdmkwyz47ondd52/gifTest2.zip?dl=0

引用的其他代码...

Other code that is referenced...

extension UIImage{
    //MARK: - Pixel
    func getColorMap() -> ColorMap {

        var colorMap = ColorMap()

        let pixelData = self.cgImage!.dataProvider!.data
        let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

        var byteIndex = 0

        for _ in 0 ..< Int(size.height){
            for _ in 0 ..< Int(size.width){

                let color = Color(red: data[byteIndex], green: data[byteIndex + 1], blue: data[byteIndex + 2])
                colorMap.colors.insert(color)
                byteIndex += 4
            }
        }

        return colorMap
    }
}

ColorMap

struct Color : Hashable {
    let red: UInt8
    let green: UInt8
    let blue: UInt8

    var hashValue: Int {
        return Int(red) + Int(green) + Int(blue)
    }

    public static func == (lhs: Color, rhs: Color) -> Bool {
        return [lhs.red, lhs.green, lhs.blue] == [rhs.red, rhs.green, rhs.blue]
    }
}


struct ColorMap {
    var colors = Set<Color>()

    var exported: Data {
        let data = Array(colors)
            .map { [$0.red, $0.green, $0.blue] }
            .joined()

        return Data(bytes: Array(data))
    }
}


推荐答案

这是iOS 11.0中的错误和11.1。 Apple已在iOS 11.2+

This is a bug in iOS 11.0 and 11.1. Apple has fixed this in iOS 11.2+

这篇关于在iOS 11中创建Gif图像颜色映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆