- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我遇到了这个非常奇怪的问题。我正在从 UIImages 创建动画 gif,大多数时候它们都是正确的。然而,当我开始使用更大尺寸的图像时,我的颜色开始消失。例如,如果我使用不超过 10 种颜色的 4 帧 32 x 32 像素图像就没问题。如果我将同一图像缩放到 832 x 832,我会失去粉红色,棕色变成绿色。
@1x 32 x 32
@10x 320 x 320
@26x 832 x 832
这是我用来创建 gif 的代码...
var kFrameCount = 0
for smdLayer in drawingToUse!.layers{
if !smdLayer.hidden {
kFrameCount += 1
}
}
let loopingProperty = [String(kCGImagePropertyGIFLoopCount): 0]
let fileProperties: [String: AnyObject] = [String(kCGImagePropertyGIFDictionary): loopingProperty as AnyObject];
let frameProperty = [String(kCGImagePropertyGIFDelayTime): Float(speedLabel.text!)!]
let frameProperties: [String: AnyObject] = [String(kCGImagePropertyGIFDictionary): frameProperty as AnyObject];
let documentsDirectoryPath = "file://\(NSTemporaryDirectory())"
if let documentsDirectoryURL = URL(string: documentsDirectoryPath){
let fileURL = documentsDirectoryURL.appendingPathComponent("\(drawing.name)\(getScaleString()).gif")
let destination = CGImageDestinationCreateWithURL(fileURL as CFURL, kUTTypeGIF, kFrameCount, nil)!
CGImageDestinationSetProperties(destination, fileProperties as CFDictionary);
for smdLayer in drawingToUse!.layers{
if !smdLayer.hidden{
let image = UIImage(smdLayer: smdLayer, alphaBlend: useAlphaLayers, backgroundColor: backgroundColorButton.backgroundColor!, scale: scale)
CGImageDestinationAddImage(destination, image.cgImage!, frameProperties as CFDictionary)
}
}
if (!CGImageDestinationFinalize(destination)) {
print("failed to finalize image destination")
}
}
我在调用 CGImageDestinationAddImage(destination, image.cgImage!, frameProperties as CFDictionary)
之前设置了一个断点,图像非常好,颜色正确。我希望有人知道我错过了什么。
更新
这是一个示例项目。请注意,虽然它在预览中没有动画,但它正在保存动画 gif 并且我在控制台中注销了图像的位置。
最佳答案
似乎关闭全局颜色图可以解决问题:
let loopingProperty: [String: AnyObject] = [
kCGImagePropertyGIFLoopCount as String: 0 as NSNumber,
kCGImagePropertyGIFHasGlobalColorMap as String: false as NSNumber
]
请注意,与 PNG 不同,GIF 只能使用 256 色贴图,没有透明度。对于动画 GIF,可以有全局或每帧颜色图。
不幸的是,Core Graphics 不允许我们直接使用颜色图,因此在编码 GIF 时会有一些自动颜色转换。
看来关掉全局色图就可以了。此外,使用 kCGImagePropertyGIFImageColorMap
为每一帧显式设置颜色映射也可能有效。
由于这似乎不能可靠地工作,让我们为每一帧创建我们自己的颜色图:
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))
}
}
现在让我们更新我们的方法:
func getScaledImages(_ scale: Int) -> [(CGImage, ColorMap)] {
var sourceImages = [UIImage]()
var result: [(CGImage, ColorMap)] = []
...
var colorMap = ColorMap()
let pixelData = imageRef.dataProvider!.data
let rawData: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)
for y in 0 ..< imageRef.height{
for _ in 0 ..< scale {
for x in 0 ..< imageRef.width{
let offset = y * imageRef.width * 4 + x * 4
let color = Color(red: rawData[offset], green: rawData[offset + 1], blue: rawData[offset + 2])
colorMap.colors.insert(color)
for _ in 0 ..< scale {
pixelPointer[byteIndex] = rawData[offset]
pixelPointer[byteIndex+1] = rawData[offset+1]
pixelPointer[byteIndex+2] = rawData[offset+2]
pixelPointer[byteIndex+3] = rawData[offset+3]
byteIndex += 4
}
}
}
}
let cgImage = context.makeImage()!
result.append((cgImage, colorMap))
和
func createAnimatedGifFromImages(_ images: [(CGImage, ColorMap)]) -> URL {
...
for (image, colorMap) in images {
let frameProperties: [String: AnyObject] = [
String(kCGImagePropertyGIFDelayTime): 0.2 as NSNumber,
String(kCGImagePropertyGIFImageColorMap): colorMap.exported as NSData
]
let properties: [String: AnyObject] = [
String(kCGImagePropertyGIFDictionary): frameProperties as AnyObject
];
CGImageDestinationAddImage(destination, image, properties as CFDictionary);
}
当然,这只有在颜色数少于 256 时才有效。我真的会推荐一个可以正确处理颜色转换的自定义 GIF 库。
关于保存动画 Gif 时 iOS 颜色不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44960714/
有谁知道是否可以将多个 gif 或动画 gif 加入到一个动画 gif 中(即,将这些帧连接到一个主动画 gif 中)? 我想要一些服务器端功能来执行此操作。 文件的尺寸、模式等将相同,只是内容不同。
提前道歉,但这不是一个真正的photoshop问题。相反,我试图想出一些令人信服的东西,但尽可能地利用 gif 格式的压缩和特性来为动画生成尽可能小的文件。 一些限制: 它需要至少 20 或 30 帧
如何创建播放一次并在最后一帧卡住的 GIF 图像。 我已经将循环属性设置为 1,所以第一个问题解决了。 但是动画结束后,gif并不是在最后一帧卡住,而是回到第一帧。 最佳答案 您需要将 gif 的循环
我有两个不同大小的 GIF。我希望能够将一个动画 GIF 放在特定位置的静态背景 GIF 上,同时将文本添加到结果中。我是 ImageMagick 世界的新手,请帮忙。 我试图实现以下结果,其中狗贴纸
你好 stackoverflow 世界。(这是我第一次在这里真正发布问题。令人兴奋) 不久前,我从我公司的一个团队那里继承了一个已有 2 年历史的 MVC 网站。我现在知道这个解决方案的大部分来龙去脉
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 1 年前。
我想将我的处理草图之一导出为 gif 形式,并使用 extrapixel 的 Gif 动画库 ( http://extrapixel.github.io/gif-animation/ ) 来执行此操作
我正在寻找一个可以处理动画 gif 图像并在其上写入文本的函数。 工作解决方案可能由 Gif4j lib 提供,但我正在寻找开源解决方案或建议如何自行实现它。 如何在 Java 中将文本放在 gif
这个问题在这里已经有了答案: Change File Extension Using C# (6 个答案) 关闭 8 年前。 此代码将重命名所有文件名: static private void Re
我会保持简短; 有什么方法可以区分静态 GIF 图像和动画图像吗?我正在使用 C#。 谢谢 最佳答案 Here's an article about how to determine the numb
我试图在视频上重叠动画 gif,但没有成功。 我的目标是下一个: gif 动画必须循环播放,直到视频结束。 gif 被缩放以覆盖整个视频。 gif 保留透明度。 我在这方面取得的最大成就是 gif 使
在您的网站上放置网站图标时,您显然可以使用动画 gif,只需将 gif 文件的扩展名更改为 .ico . http://www.k-director.com/blog/how-to-add-an-an
所以我试图为一个充满 gif 的文件夹添加水印,但我收到一条错误消息,说我当时只能使用一个 GIF 流,有没有办法绕过这个问题? @echo off setlocal for %%G in ("%~d
我有大约 200 张 jpg 图像。我需要堆叠它们,以便我可以将它们转换为简单的动画 gif 图像。是否有任何免费工具可以完成这项工作?我的操作系统是windows。 我不太关心输出的质量。 最佳答案
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我想使用库显示 GIF WPF Animated GIF 。但是,当设置属性 PictureSource 时,进程内存会从 208MB 增加到 1GB。为什么? XAML
几天后我有话要说。我必须引用细胞原子。我想在显示元胞自动机进化的幻灯片中显示一个小 gif,所以我的问题是:如何将使用 golly game of life 创建的模式和进化转换为动画 gif? 最佳
看这段代码: $('#loader').show(); $.post( '/action.php', function( data ) { // do anything with data $('#
作为项目的一部分,我们需要以编程方式将多个动画 GIF 以网格的形式组合成一个主动画 GIF(一个 gif 文件)。 我们不关心它是在客户端(即带有 ios/android 的智能手机)还是在服务器端
我正在制作一个小游戏。这不是 Action 游戏,而是解谜游戏,因此性能并不是那么重要。现在,我有了主游戏区,一张背景图片。在某些情况下,我想在部分背景图像上绘制其他图像。我的问题是背景图片和叠加的图
我是一名优秀的程序员,十分优秀!