Unable to show a *.gif file in a UIImageView in sw

2019-04-28 21:02发布

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.

3条回答
姐就是有狂的资本
2楼-- · 2019-04-28 21:45

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

install pod 'SDWebImage'

查看更多
地球回转人心会变
3楼-- · 2019-04-28 21:53
//
//  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.

查看更多
地球回转人心会变
4楼-- · 2019-04-28 21:54

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)
查看更多
登录 后发表回答