Reputation: 9705
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.
Upvotes: 6
Views: 23065
Reputation: 558
//
// 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.
Upvotes: 0
Reputation: 8057
In my case i'm using SDWebImage
for manage all image in app. Inclusive have sd_animatedGIFNamed
func
install pod 'SDWebImage'
Upvotes: 1
Reputation: 166
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)
Upvotes: 12