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

2

9

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?

Alvinia answered 24/11, 2017 at 7:33 Comment(0)
M
17

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")
    
Mucro answered 24/11, 2017 at 9:9 Comment(6)
Thanks, just what I needed. Although it is not possible to add 2x and 3x GIF in the same dataset is it?Alvinia
@Amogh I guess It should be possible (I tried in my local test)Mucro
how are you doing it? I am using xcode 7.3 and I am not getting option to add another GIF to the same datasetAlvinia
I am running Xcode9 and I have possibility to add 1,2,3x. Check to have selected "Individual Scales" for "Image Set/Scales"Mucro
could you please tell me the code in your Contents.json file?Alvinia
{ "images" : [ { "idiom" : "universal", "filename" : "mygif.gif", "scale" : "1x" }, { "idiom" : "universal", "filename" : "[email protected]", "scale" : "2x" }, { "idiom" : "universal", "filename" : "[email protected]", "scale" : "3x" } ], "info" : { "version" : 1, "author" : "xcode" } }Mucro
Z
5

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

Zoochore answered 24/11, 2017 at 7:37 Comment(6)
I want to load GIF from Assets folder onlyAlvinia
give gif name directly which is in your assetZoochore
@Ganesh the library seems to be not supporting asset name, only full path from bundleMucro
@AndreaMugnaini library will support asset name check it clearlyZoochore
@Ganesh I checked again, according to this: github.com/bahlo/SwiftGif/blob/master/SwiftGifCommon/… UIImage can be initialized only from: Data, url as String, or bundleURL as String. So there is no way to use resource from *.xcassets furthermore you can try making a test project and see that you'll receive: SwiftGif: This image named "YOUR_GIF_NAME" does not existMucro
any solution to load gif from Assets folder to FLAnimatedImage? Data(contentsOf: Bundle.main.url(forResource: "logo-animation", withExtension: "gif") doesn't work for assets folder accessCarillo

© 2022 - 2024 — McMap. All rights reserved.