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

天涯浪子 提交于 2020-01-02 03:48:11

问题


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.


回答1:


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)



回答2:


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

install pod 'SDWebImage'




回答3:


//
//  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.



来源:https://stackoverflow.com/questions/33229145/unable-to-show-a-gif-file-in-a-uiimageview-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!