Load GIF Image in Swift.

Hi, It’s very simple to set PNG or JPG image in imageView. You can directly set using image name or image URL. In this tutorial provide How to load GIF image in UIImageView in Swift.  



Step 1 : We are already create file name as iOSDevCenters+GIF.swift in which create an extension of UIImage for load GIF images.Download the demo project and use below method to load GIF. 

Step 2 : The iOSDevCenters+GIF.swift file have three methods. You can load GIF image of its name, or Converting GIF image to Data or Also load GIF from URL.

Step 3 : Load GIF image of Name :
let jeremyGif = UIImage.gifImageWithName("funny")
let imageView = UIImageView(image: jeremyGif)
imageView.frame = CGRect(x: 20.0, y: 50.0, width: self.view.frame.size.width - 40, height: 150.0)
view.addSubview(imageView)

Step 4 : Load GIF Image using NSData :
let imageData = NSData(contentsOfURL: NSBundle.mainBundle().URLForResource("play", withExtension: "gif")!)
let advTimeGif = UIImage.gifImageWithData(imageData!)
let imageView2 = UIImageView(image: advTimeGif)
imageView2.frame = CGRect(x: 20.0, y: 220.0, width: self.view.frame.size.width - 40, height: 150.0)
view.addSubview(imageView2)

Step 5 : Load GIF image from URL :
let gifURL : String = "http://www.gifbin.com/bin/4802swswsw04.gif"
let imageURL = UIImage.gifImageWithURL(gifURL)
let imageView3 = UIImageView(image: imageURL)
imageView3.frame = CGRect(x: 20.0, y: 390.0, width: self.view.frame.size.width - 40, height: 150.0)
view.addSubview(imageView3)

GIF-Swift Demo

Thanks :
Load GIF Image in Swift. Load GIF Image in Swift. Reviewed by Kirit Modi on 02:48:00 Rating: 5

No comments:

Powered by Blogger.