使用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都会以相同的延迟时间运行。此外,如果我将fileProperties
中的值从0更改为任何其他数字,GIF仍会进行连续循环。
我觉得错误在于
行var destination2: Void = CGImageDestinationSetProperties(destination, fileProperties)
或我对fileProperties
和frameProperties
有人知道为什么我的fileProperties
和frameProperties
没有影响GIF吗?
P.S。我可以通过增加帧并将imagessss
数组与重复图像相结合来有效地减慢gif,但这并不理想。