为什么 GIF 不会随着 kCGImagePropertyGIFDelayTime 变慢

Why won't GIF slow down with kCGImagePropertyGIFDelayTime

使用 Rob Mayoff's answer to create GIF,我已将他的代码修改为 Swift 中的以下代码。

let kFrameCount:Int = 6

let frames:NSNumber = NSNumber(float: 20000.0) //No matter what number I place here...GIF runs at same speed

let fileProperties = [kCGImagePropertyGIFLoopCount as String: 0]
let frameProperties = [kCGImagePropertyGIFDelayTime as String: frames]

let documentsUrl =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as! NSURL
let fileURL = documentsUrl.URLByAppendingPathComponent("animated.gif")

var destination: CGImageDestination = CGImageDestinationCreateWithURL(fileURL, kUTTypeGIF, kFrameCount, nil)
var destination2: Void = CGImageDestinationSetProperties(destination, fileProperties)

for i in 0..<kFrameCount {
   CGImageDestinationAddImage(destination, imagessss[i].CGImage, frameProperties)
}

if (!CGImageDestinationFinalize(destination)) {
        println("fail")
}

println(fileURL)

let activityVC: UIActivityViewController = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)

这将创建 GIF。但无论我设置多少 float、20000.0 或 0.01,GIF 都以相同的 DelayTime 运行。此外,如果我将 fileProperties 中的值从 0 更改为任何其他数字,GIF 仍然会连续循环。

我觉得错误在于

var destination2: Void = CGImageDestinationSetProperties(destination, fileProperties)

或我对 filePropertiesframeProperties

的声明

有人知道为什么我的 filePropertiesframeProperties 没有影响 GIF 吗?

P.S。我可以通过增加帧数并在 imagessss 数组中添加重复图像来有效地减慢 gif,但这并不理想。

您的属性中缺少 kCGImagePropertyGIFDictionary 级别。

将您的属性更改为:

let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]] let frameProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: frames]]

(我正在使用 Xcode 6.4)