在Swift中创建GIF动画的问题 [英] Problems creating an animated gif in Swift

查看:179
本文介绍了在Swift中创建GIF动画的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Swift中的4个 UIImage 元素的数组创建一个动画gif,但目前只保存第一帧。

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
}

照片是数组 UIImages
filename()只返回一个字符串,如 20150805。 gif

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

它返回true但只有第一帧在gif中

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

推荐答案

kCGImagePropertyGIFDelayTime
应该是浮动而不是双倍。

kCGImagePropertyGIFDelayTime should be in float not double.

尝试设置0.125f而不是0.125

try setting 0.125f instead of 0.125

这篇关于在Swift中创建GIF动画的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆