2015-10-20 16 views
6

Tôi có tệp * .gif mà tôi muốn hiển thị trong một UIImageView. Tôi đã thử thư viện FLAnimatedImage, mã bên dưới. Kết quả chỉ là một hình ảnh tĩnh.Không thể hiển thị tệp * .gif trong UIImageView trong 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 
    } 
} 

Mở mọi cách thay thế để hiển thị gif động trực tiếp từ tệp gif. Tôi đang sử dụng Swift.

+0

bạn có thể sử dụng thư viện https://github.com/Flipboard/FLAnimatedImage –

+0

tải hình ảnh GIF này trong Swift: https://iosdevcenters.blogspot.com/2016/08/load-gif-image-in-swift_22. html –

Trả lời

9

Tôi khuyên bạn nên sử dụng SwiftGif.

nhập các Gif.swift trong dự án của bạn và thực hiện như sau:

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

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

Hoặc bạn có thể kiểm tra [this] (http://stackoverflow.com/questions/24485308/how-to-show-gif-into-uiimageview-in-swift). –

0

Trong trường hợp của tôi, tôi đang sử dụng SDWebImage cho quản lý tất cả hình ảnh trong ứng dụng. Inclusive có sd_animatedGIFNamed func

cài đặt 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. 
    } 




} 

Tạo một bộ điều khiển xem như thể hiện trong đoạn code trên. Lưu ý: - trong khi tạo một lần xem hình ảnh trong bảng câu chuyện, hãy đặt lớp chế độ xem hình ảnh là FLanimatedImageView.

Nó sẽ hoạt động.

Ở đây Loader.gif là hình ảnh GIF.

Các vấn đề liên quan