【问题标题】:Gif disappears in UITableViewCellGif 在 UITableViewCell 中消失
【发布时间】:2020-02-10 13:18:23
【问题描述】:

我正在使用这个问题的答案:How to load GIF image in Swift?,我正在将 gif 添加到 UITableViewCell 中。当您打开表格时,它可以工作并且正在制作动画,但是当我转到另一个 UIViewController 并返回 UITableViewController 时,gif 不存在。它仅在您 .touchUpOutside UITableViewCell 时出现。如何解决这个问题?

class CustomCell: UITableViewCell{

    @IBOutlet weak var theImageView: UIImageView!{
        didSet{
            let loadingGif = UIImage.gifImageWithName("loading")
            theImageView.image = loadingGif
        }
    }
}

【问题讨论】:

  • 你如何“去另一个 UIViewController”?您使用的是 segues 还是导航控制器或什么?您可能正在创建包含表视图的 UIViewController 的第二个实例。看看这篇关于转入视图控制器然后转回的帖子是否有用:stackoverflow.com/questions/58260379/…
  • 你能放一些代码吗?
  • 使用 BarButtonItem 或 UITableViewCell 点击没关系。当 UITableViewController 消失时,gif 也会消失。使用上面的代码并将 gif 添加到 UITableViewCell 以查看。

标签: swift uitableview uiimage gif


【解决方案1】:

我将展示在 UITableViewCell 上显示动画 gif 的所有阶段

1) 这是我的 GifTableViewController,它包含一个 UITableView

import UIKit

class GifTableViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    var gifs = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        loadGifs()
    }

    func loadGifs() {
        gifs.append("https://media.giphy.com/media/XIqCQx02E1U9W/giphy.gif")
        gifs.append("https://media.giphy.com/media/11JTxkrmq4bGE0/giphy.gif")
        gifs.append("https://media.giphy.com/media/eoxomXXVL2S0E/giphy.gif")
        gifs.append("https://media.giphy.com/media/c5wbvuaVVLWzC/giphy.gif")
        gifs.append("https://media.giphy.com/media/l9Jhzwdi09Ve0/giphy.gif")
        gifs.append("https://media.giphy.com/media/8h1Zhv62CVXEc/giphy.gif")
        gifs.append("https://media.giphy.com/media/FgiHOQyKUJmwg/giphy.gif")
        gifs.append("https://media.giphy.com/media/h2MLtoOjxtkGY/giphy.gif")
        gifs.append("https://media.giphy.com/media/ClKnUxoh4SP16/giphy.gif")
        gifs.append("https://media.giphy.com/media/S6fA9ppFTwFhK/giphy.gif")
        gifs.append("https://media.giphy.com/media/EGiBhTZMXedIA/giphy.gif")
    }

}


extension GifTableViewController: UITableViewDataSource, UITableViewDelegate {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return gifs.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "GifTableCell", for: indexPath) as! GifTableCell
        cell.load(with: gifs[indexPath.row])
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 200
    }

}

2) 这是我的 GifTableCell,其中包含一个 UIImageView,它将代表 gif

import UIKit

class GifTableCell: UITableViewCell {

    @IBOutlet weak var gifImageView: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func load(with urlString: String) {
        gifImageView.image = nil
        DispatchQueue.global().async { [weak self] in
            guard let url = URL(string: urlString as String) else {
                return
            }
            guard let data = try? Data(contentsOf: url) else {
                return
            }

            DispatchQueue.main.async {
                self?.gifImageView.image = UIImage.gif(data: data)
            }
        }
    }

}

3) 注意UIImage.gif(data: data) 语句。 gif 函数是来自 SwiftGifOrigin 库的 UIImage 扩展

查看来源:https://github.com/swiftgif/SwiftGif

您可以只添加UIImage+Gif.swift 文件以便简单地使用,或者将SwiftGifOrigin 库包含到您的项目中。

问题更新编辑;

上面的例子显示 gif 是从 url 加载的。你的case其实简单多了,你的cell应该是这样的。

class CustomCell: UITableViewCell{

    @IBOutlet weak var theImageView: UIImageView!

    func loadGif() {
        theImageView.image = nil
        DispatchQueue.global().async { [weak self] in
            let loadingGif = UIImage.gifImageWithName("loading")
            DispatchQueue.main.async {
                self?.theImageView.image = loadingGif
            }
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-26
    • 2021-12-05
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多