Load Animated gif in UIImageView IOS

0 votes

I have this gif into my assets file Gif

The name of the assets is loading_apple but when I add the following code I get a nullpointer exception error:

let img = UIImage (named: "loading_apple")

So how can I show this gif ? :(

Hope someone can help me.
 

Sep 21, 2022 in Others by gaurav
• 23,260 points
8,146 views

1 answer to this question.

0 votes

Load GIF image Swift :

## Reference.

#1 : Copy the swift file from This Link :

#2 : Load GIF image Using 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)

#3 : Load GIF image Using Data

    let imageData = try? Data(contentsOf: Bundle.main.url(forResource: "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)

#4 : Load GIF image Using 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)

Download Demo Code

OUTPUT :

iPhone 8 / iOS 11 / xCode 9

enter image description here

answered Sep 21, 2022 by rajatha
• 7,680 points

Related Questions In Others

0 votes
1 answer
0 votes
0 answers

IOS - Print image to Phomemo M02 Mini Bluetooth Thermal Printer using CoreBluetooth in Swift 5?

I have a Phomemo M02 Mini Bluetooth Thermal ...READ MORE

Sep 22, 2022 in Others by gaurav
• 23,260 points
589 views
0 votes
0 answers

How to build a font generator in iOS Swift and allowing pasting to clipboard

I'm now building a listview to display ...READ MORE

Sep 23, 2022 in Others by gaurav
• 23,260 points
337 views
0 votes
1 answer

How to Paste JPEG as a gif in excel?

Solution  Step 1 Navigate to the folder that contains ...READ MORE

answered Oct 2, 2022 in Others by narikkadan
• 63,700 points
428 views
0 votes
1 answer

reduce camera/photo library image file size for less than 100 KB in iphone

The simplest technique to reduce the size ...READ MORE

answered Nov 8, 2022 in IOS by Rahul
• 9,670 points
578 views
0 votes
1 answer

How do I create folder under an Amazon S3 bucket through PHP API?

Of Course, it is possible to create ...READ MORE

answered Apr 24, 2018 in AWS by anonymous
11,025 views
0 votes
1 answer

Detecting BLE beacons that are not ibeacons on IOS

You'll have to understand that CoreLocation is ...READ MORE

answered Aug 1, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
704 views
0 votes
1 answer

Ionic capacitor app shows only white blank screen in ios

Try it: In capacitor.config.json file, we have ...READ MORE

answered Sep 21, 2022 in Others by rajatha
• 7,680 points
3,067 views
0 votes
1 answer

ios: localize emoji name (kCFStringTransformToUnicodeName)

There is no api for this job. Unicode ...READ MORE

answered Sep 21, 2022 in Others by rajatha
• 7,680 points
783 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP