Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add GIF images to Assets folder and load them in UIImageView programmatically

I am trying to put my @2x and @3x GIF images into the Assets folder in Xcode. I have tried the following links but it didn't work for me. Link 1 and Link 2.

I am currently loading the GIF files by adding them to my project bundle and accessing it using this

let bundleURL = NSBundle.mainBundle()
            .URLForResource("phone_animation", withExtension: "gif") 

But I want to load them from my Assets folder. Is there a way I can do it? And how do I load them into my imageview after adding it to Assets?

like image 399
Amogh Shettigar Avatar asked Nov 24 '17 07:11

Amogh Shettigar


People also ask

Can you use gifs in Xcode?

With the release of Xcode 12.5, Apple finally lets developers record videos and animated GIF right from simulators. In this tutorial, I will walk you through this new feature. Please note that the latest version of Xcode requires you to run macOS Big Sur.


2 Answers

According to your comment, this is the solution that you are searching:

  1. make an extension of UIImage which uses SwiftGif:

    extension UIImage {
      public class func gif(asset: String) -> UIImage? {
        if let asset = NSDataAsset(name: asset) {
           return UIImage.gif(data: asset.data)
        }
        return nil
      }
    }
    
  2. Then tuning @Ganesh suggestion with the extension you might do something like:

    imageView.image = UIImage.gif(asset: "YOUR_GIF_NAME_FROM_ASSET")
    
like image 193
Andrea Mugnaini Avatar answered Sep 18 '22 22:09

Andrea Mugnaini


To load gif into UIImage I suggest two libraries

1) you can use this library

simply you can load it into UIImageView

let imageData = try! Data(contentsOf: Bundle.main.url(forResource: "logo-animation", withExtension: "gif")!)
let gifImage = UIImage.gif(data: data!)
imageView.image = gifImage

2) Or you can use this library

import FLAnimatedImage

then add outlet

@IBOutlet weak var fashImage: FLAnimatedImageView!

then use this code

let imageData = try! Data(contentsOf: Bundle.main.url(forResource: "logo-animation", withExtension: "gif")!)
fashImage.animatedImage = FLAnimatedImage(animatedGIFData: imageData)

Hope this will help you

like image 36
Ganesh Manickam Avatar answered Sep 16 '22 22:09

Ganesh Manickam