在Swift中创建GIF动画的问题

时间:2015-08-06 02:43:09

标签: ios swift gif

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

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
}

photosUIImages的数组 filename()只返回20150805.gif

之类的字符串

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

1 个答案:

答案 0 :(得分:2)

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

尝试设置0.125f而不是0.125