2015-10-20 28 views
6

Ho un file * .gif che voglio mostrare in un UIImageView. Ho provato la libreria FLAnimatedImage, codice qui sotto. Il risultato è solo un'immagine statica.Impossibile mostrare un file * .gif in un UIImageView in swift

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

Aperto a tutti i modi alternativi per mostrare animazioni gif direttamente da un file GIF. Sto usando Swift.

+0

è possibile utilizzare questa libreria https://github.com/Flipboard/FLAnimatedImage –

+0

caricare l'immagine GIF in Swift: https://iosdevcenters.blogspot.com/2016/08/load-gif-image-in-swift_22. html –

risposta

9

Si consiglia vivamente di utilizzare SwiftGif.

Importare il Gif.swift nel progetto e procedere come segue:

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

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

Oppure puoi controllare [questo] (http://stackoverflow.com/questions/24485308/how-to-show-gif-into-uiimageview-in-swift). –

0

Nel mio caso sto usando SDWebImage per gestire tutti image in app. Inclusiva hanno sd_animatedGIFNamed Funz

installare pod 'SDWebImage'

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




} 

Creare un controller di vista, come illustrato nel codice precedente. Nota: - durante la creazione di una visualizzazione di immagini nella story board, impostare la classe di visualizzazione dell'immagine come FLanimatedImageView.

Funzionerà.

Qui Loader.gif è l'immagine GIF.