Problems creating an animated gif in Swift

最后都变了- 提交于 2020-01-23 01:57:45

问题


I am trying to create an animated gif from an array of 4 UIImage elements in Swift but currently it only saves the first frame.

let url = NSURL(fileURLWithPath: photosDirectory)?.URLByAppendingPathComponent(filename())

if let url = url {
    let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]]
    let gifProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: 0.125]]
    let destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, photos.count, nil)

    CGImageDestinationSetProperties(destination, fileProperties)

    for photo in photos {
        CGImageDestinationAddImage(destination, photo.CGImage, gifProperties)
    }

    return CGImageDestinationFinalize(destination)
}
else {
    return false
}

photos is the array of UIImages filename() just returns a string like 20150805.gif

It returns true but only the first frame is in the gif


回答1:


kCGImagePropertyGIFDelayTime should be in float not double.

try setting 0.125f instead of 0.125



来源:https://stackoverflow.com/questions/31845981/problems-creating-an-animated-gif-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!