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?

Amogh Shettigar
  • 275
  • 2
  • 3
  • 18

2 Answers2

16

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")
    
Zorayr
  • 23,770
  • 8
  • 136
  • 129
mugx
  • 9,869
  • 3
  • 43
  • 55
  • Thanks, just what I needed. Although it is not possible to add 2x and 3x GIF in the same dataset is it? – Amogh Shettigar Nov 24 '17 at 11:41
  • @Amogh I guess It should be possible (I tried in my local test) – mugx Nov 24 '17 at 12:08
  • how are you doing it? I am using xcode 7.3 and I am not getting option to add another GIF to the same dataset – Amogh Shettigar Nov 24 '17 at 12:40
  • I am running Xcode9 and I have possibility to add 1,2,3x. Check to have selected "Individual Scales" for "Image Set/Scales" – mugx Nov 24 '17 at 13:29
  • could you please tell me the code in your Contents.json file? – Amogh Shettigar Nov 27 '17 at 10:25
  • { "images" : [ { "idiom" : "universal", "filename" : "mygif.gif", "scale" : "1x" }, { "idiom" : "universal", "filename" : "mygif@2x.gif", "scale" : "2x" }, { "idiom" : "universal", "filename" : "mygif@3x.gif", "scale" : "3x" } ], "info" : { "version" : 1, "author" : "xcode" } } – mugx Nov 27 '17 at 11:46
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

Ganesh Manickam
  • 2,113
  • 3
  • 20
  • 28
  • I want to load GIF from Assets folder only – Amogh Shettigar Nov 24 '17 at 08:34
  • 1
    give gif name directly which is in your asset – Ganesh Manickam Nov 24 '17 at 09:02
  • @Ganesh the library seems to be not supporting asset name, only full path from bundle – mugx Nov 24 '17 at 09:14
  • @AndreaMugnaini library will support asset name check it clearly – Ganesh Manickam Nov 24 '17 at 09:18
  • @Ganesh I checked again, according to this: https://github.com/bahlo/SwiftGif/blob/master/SwiftGifCommon/UIImage%2BGif.swift 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 exist – mugx Nov 24 '17 at 09:27
  • 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 access – tech savvy Oct 17 '18 at 20:05