6

I have a *.gif file that I want to show in a UIImageView. I have tried the library FLAnimatedImage, code below. Result is just a static image.

class LoginVC: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var img_popup: FLAnimatedImageView!

    var img_popupraw: FLAnimatedImage = FLAnimatedImage(animatedGIFData: NSData(contentsOfFile: "ShitTalk_LoadingAnimation.gif"))

    override func viewDidLoad() {
        img_popup.animatedImage = img_popupraw
    }
}

Open to any alternative ways to show animating gif directly from a gif file. I am using Swift.

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
user1406716
  • 9,565
  • 22
  • 96
  • 151

3 Answers3

12

I strongly recommend you to use SwiftGif.

Import the Gif.swift in your project and do the following:

// Returns an animated UIImage
let jeremyGif = UIImage.gifWithName("jeremy")

// Use the UIImage in your UIImageView
let imageView = UIImageView(image: jeremyGif)
Barat Semet
  • 156
  • 1
  • 5
  • Or you may check [this](http://stackoverflow.com/questions/24485308/how-to-show-gif-into-uiimageview-in-swift) out. – Barat Semet Oct 20 '15 at 07:15
1

In my case i'm using SDWebImage for manage all image in app. Inclusive have sd_animatedGIFNamed func

install pod 'SDWebImage'

jose920405
  • 7,982
  • 6
  • 45
  • 71
0
//
//  ViewController.swift
//  Loader
//
//  Created by Pawan Kumar on 28/09/17.
//  Copyright © 2017 Pawan Kumar. All rights reserved.
//

import UIKit
import FLAnimatedImage

class ViewController: UIViewController {

    @IBOutlet weak var animatedImageView: FLAnimatedImageView!
//  @IBOutlet weak var checkImageView: FLAnimatedImageView!
    override func viewDidLoad() {
        super.viewDidLoad()


        let url  = Bundle.main.path(forResource: "Loader", ofType: "gif")
        print(url!)

        let data=NSData(contentsOfFile: url!)
        //print(data)
        let fff=FLAnimatedImage(gifData: data as Data?)
        let imageView=FLAnimatedImageView()
        imageView.animatedImage=fff
//
//        imageView.frame=CGRect(x: animatedImageView.frame.minX, y: animatedImageView.frame.minY, width: animatedImageView.frame.width, height: animatedImageView.frame.height)


         imageView.frame=CGRect(x: animatedImageView.frame.minX, y: animatedImageView.frame.minY, width: animatedImageView.frame.width, height: animatedImageView.frame.height)

        print(imageView.currentFrameIndex)
        imageView.clipsToBounds=true
        self.view.addSubview(imageView)

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }




}

Create a view controller as shown in the code above. Note:- while creating an imageview in story board, set the class of image view as FLanimatedImageView.

It will work.

Here Loader.gif is the GIF image.

JSON C11
  • 11,272
  • 7
  • 78
  • 65
Pawan Kumar
  • 508
  • 3
  • 19