在 iOS 11 中创建 Gif 图像颜色映射
Creating Gif Image Color Maps in iOS 11
我最近在创建 Gif 时遇到问题,如果它太大,颜色就会丢失。但是,感谢 SO 的帮助,有人能够帮助我找到解决方法并创建我自己的颜色图。
上一个问题在这里...
在 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: "")!
}
这里是link下载一个测试项目。请注意,如果您在 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 { [[=13=].red, [=13=].green, [=13=].blue] }
.joined()
return Data(bytes: Array(data))
}
}
我看到了 zip 项目。好像不是 "swifty".. :)
总之:
- 下面您可以找到适用于 iOS 11 的最少代码。
我们可以从这里开始
问题:
1) 不同的尺寸应该怎么办?我们想在新的大图像中调整 GIF 大小,在原始图像周围制作黑色区域
2) 你能给我更多关于彩色地图的细节吗?
3) 矩阵都是什么东西?好像要填黑色?这不是明智而快捷的方法。我将使用 Apple 函数缩放 up/fill 背景。
只要您好心回答我就可以帮助您。
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)")
}
}
这是 iOS 11.0 和 11.1 中的错误。 Apple 已在 iOS 11.2+
中修复此问题
我最近在创建 Gif 时遇到问题,如果它太大,颜色就会丢失。但是,感谢 SO 的帮助,有人能够帮助我找到解决方法并创建我自己的颜色图。
上一个问题在这里...
在 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: "")!
}
这里是link下载一个测试项目。请注意,如果您在 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 { [[=13=].red, [=13=].green, [=13=].blue] }
.joined()
return Data(bytes: Array(data))
}
}
我看到了 zip 项目。好像不是 "swifty".. :)
总之:
- 下面您可以找到适用于 iOS 11 的最少代码。
我们可以从这里开始
问题:
1) 不同的尺寸应该怎么办?我们想在新的大图像中调整 GIF 大小,在原始图像周围制作黑色区域
2) 你能给我更多关于彩色地图的细节吗?
3) 矩阵都是什么东西?好像要填黑色?这不是明智而快捷的方法。我将使用 Apple 函数缩放 up/fill 背景。
只要您好心回答我就可以帮助您。
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)")
}
}
这是 iOS 11.0 和 11.1 中的错误。 Apple 已在 iOS 11.2+
中修复此问题