使用SwiftGif进行慢速gif动画

时间:2016-06-09 18:55:10

标签: ios swift uiimage gif animated-gif

我使用这个SwiftGif库。我把一些gif放到UIImageView中。这是我的代码:

dispatch_async(dispatch_get_main_queue(),{
    self.img1.image=UIImage.gifWithURL("http://mywebsite/img1.gif")
    self.img2.image=UIImage.gifWithURL("http://mywebsite/img2.gif")
    self.img3.image=UIImage.gifWithURL("http://mywebsite/img1.gif")
})

GIF正确显示,但动画很慢。我怎样才能解决这个问题?谢谢!

3 个答案:

答案 0 :(得分:4)

将以下代码粘贴到UIImage+Gif.swift中,在animatedImageWithSource函数中完成持续时间的计算:

 // Calculate full duration
let duration: Int = {
    var sum:Double = 0

    for val: Int in delays {
        let newVal =    Double(val) - (Double(val)/1.5)//Modified calculation to speed up the animtion in gif
        //sum += val :default calculation
        sum += newVal
    }

    return Int(sum)
    }()

答案 1 :(得分:0)

我假设你的gif文件太大了?还是时间太长了?因为SwiftGif基于UIAnimatedImage。

并控制9,10,20,30,40的gif帧,并将GCD设置为1。

答案 2 :(得分:0)

如果你的gif尺寸过大,请使用 webview 加载gif

添加WebView,

将gif文件添加到项目目录中,然后添加以下代码:

let url = Bundle.main.url(forResource: "your-gif-name", withExtension: "gif")!
let data = try! Data(contentsOf: url)
webView.load(data, mimeType: "image/gif", textEncodingName: "UTF-8", baseURL: NSURL() as URL)

注意:使用任何gif编辑网站将您的gif文件的高度宽度 webview 相同