在iOS中创建Gif图像颜色地图11
问题描述:
我最近在创建Gif时遇到了问题,如果它有太多的颜色丢失。不过感谢来自这里的帮助,有人能够帮助我找到解决办法并创建我自己的色彩地图。在iOS中创建Gif图像颜色地图11
上的问题在这里... iOS Colors Incorrect When Saving Animated Gif
这很好工作,直到iOS的11.我找不到更改过的文档任何东西,将使这个不再工作。我发现的是,如果我删除kCGImagePropertyGIFImageColorMap
gif生成,但它有原始问题,如果gif变得像我以前的问题一样大,颜色就会丢失。这是有道理的,因为这是为了解决这个问题而添加的。
疑似问题...
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上运行它,它是一个白色图像。
https://www.dropbox.com/s/hdmkwyz47ondd52/gifTest2.zip?dl=0
所引用的其他代码...
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
}
}
颜色表
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的一个错误。苹果已经在iOS 11.2+中修复了这个问题
答
我看到拉链项目。它似乎不是 “SWIFTY” .. :)
总之:
- 下面你可以找到最小的代码,适用于iOS 11.工作
,我们可以从这个
-
的问题开始:
1)不同尺寸会发生什么?我们在一个新的大图像中调整原始图像周围的黑色区域的GIF大小
2)你能给我更多关于彩色地图的细节吗?
3)什么是矩阵的所有东西?看来你想填黑?这不是聪明和快捷的方法。我将使用Apple功能来放大/填充背景。
我可以帮助你一次您已经好心回答。
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if let image = UIImage(named: "image1"){
createGIFFrom(image: image)
}
}
final func localPath()->URL{
let tempPath = NSTemporaryDirectory()
let url = URL.init(fileURLWithPath: tempPath)
return url.appendingPathComponent("test.gif")
}
private final func createGIFFrom(image: UIImage){
let fileURL = self.localPath()
let type: CFString = kUTTypeGIF// kUTTypePNG
// from apple code:
//https://developer.apple.com/library/content/technotes/tn2313/_index.html
guard let myImageDest = CGImageDestinationCreateWithURL(fileURL as CFURL, type, 1, nil) else{
return
}
guard let imageRef = image.cgImage else{
return
}
// Add an image to the image destination
CGImageDestinationAddImage(myImageDest, imageRef, nil)
CGImageDestinationFinalize(myImageDest)
print("open image at: \(fileURL)")
}
}
不幸的是,这并没有帮助。问题是创建更大的GIF时,颜色因我的原始SO问题链接而丢失。添加彩色贴图解决了这个问题,但它不再适用于iOS 11.这个答案没有解决任何问题。我正在寻找一个解决iOS 11中缺失颜色问题的答案。 –
现在更清洁一些。我没有阅读原文,抱歉。 所以主要的目标是缩放GIF,我说得对吗? (样本中每4个按钮...) – ingconti
样本将它们缩放而不缩放。问题是保存较大的GIF颜色时丢失了。 –