最近,我在创建Gif时遇到了一个问题,如果它的颜色太大,就会丢失。然而,感谢帮助,所以有人能够帮助我找到一个工作,并创建我自己的颜色地图。
前面的问题.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
}
}
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))
}
}
发布于 2018-03-07 20:23:56
这是iOS 11.0和11.1中的一个bug。苹果已经在iOS 11.2+中修复了这个问题。
发布于 2017-09-30 18:23:49
我看到了邮编项目。它似乎不是“猛击”。:)
总之:
只要你有好意的回答,我就能帮你。
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)")
}
}
https://stackoverflow.com/questions/46391686
复制相似问题