使用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.0 1)放置多少,GIF都以相同的DelayTime运行。另外,如果我将fileProperties
中的值从0更改为任何其他数字,则GIF仍然会执行一个连续循环。
我觉得错误就在线上
var destination2: Void = CGImageDestinationSetProperties(destination, fileProperties)
或者我对fileProperties
和frameProperties
的声明
有人知道为什么我的fileProperties
和frameProperties
没有影响GIF吗?
我可以通过增加帧和用重复图像绑定imagessss
数组来有效地减缓gif,但这并不理想。
发布于 2015-09-09 13:52:45
属性中缺少kCGImagePropertyGIFDictionary
级别。
将属性更改为:
let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]] let frameProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: frames]]
(我使用的是Xcode 6.4)
https://stackoverflow.com/questions/30481826
复制相似问题