Reputation: 573
i use this SwiftGif library. I put some gif into UIImageView. This is my code:
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")
})
The gifs are shown correctly but the animation is slow. How can I fix this? Thanks!
Upvotes: 4
Views: 4052
Reputation: 411
if your gif size is too big then Use a webview to load gif
Add a WebView,
Add gif file to your project directory then add this following code :
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)
Note: Make your gif file's height and width as same as the webview using any gif editing website
Upvotes: 2
Reputation: 111
Paste the following code in UIImage+Gif.swift
where calculation of duration is done in in animatedImageWithSource
function:
// 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)
}()
Upvotes: 8
Reputation: 104
I assume that your gif file size is too big? or the time is too long? because SwiftGif is base on UIAnimatedImage.
And also control your gif frame of 9, 10, 20, 30, 40, and setup GCD to 1.
Upvotes: 0