无法在swift中的UIImageView中显示* .gif文件

时间:2015-10-20 06:04:19

标签: ios swift uiimageview gif

我想要在UIImageView中显示* .gif文件。我已经尝试了库FLAnimatedImage,代码如下。结果只是一个静态图像。

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
    }
}

可以直接从gif文件中以任何其他方式显示动画gif。我正在使用Swift。

3 个答案:

答案 0 :(得分:10)

我强烈建议您使用SwiftGif

导入项目中的Gif.swift并执行以下操作:

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

// Use the UIImage in your UIImageView
let imageView = UIImageView(image: jeremyGif)

答案 1 :(得分:0)

在我的情况下,我使用SDWebImage来管理应用中的所有图片。包含有sd_animatedGIFNamed func

安装pod 'SDWebImage'

答案 2 :(得分: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.
    }




}

创建一个视图控制器,如上面的代码所示。 注意: - 在故事板中创建图像视图时,将图像视图的类设置为FLanimatedImageView。

它会起作用。

此处 Loader.gif 是GIF图片。