首页 > 解决方案 > 从 api 获取 gif 图像并显示到 tableview 中

问题描述

如果有人有好的想法来实现这个让我知道,谢谢

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    @IBOutlet weak var tableView: UITableView!

    @IBOutlet weak var tableViewCell: UITableViewCell!
    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "trendingCell", for: indexPath)

        print(self.array[indexPath.row])

        cell.textLabel?.text = (self.array[indexPath.row]["title"] as! String)

        cell.detailTextLabel?.text = (self.array[indexPath.row]["username"] as! String)

        Alamofire.request(imageUrl!, method: .get).response { response in
            guard let image = UIImage(data:response.data!) else {
                // Handle error
                return
            }
            let imageData = UIImageJPEGRepresentation(image,1.0)
            cell.myImage.image = UIImage(data : imageData!)
        }

        return cell
    }
}

标签: iosswift

解决方案


您正在强制解开一些值。尝试安全地解开这些值。不过,您仍然必须弄清楚为什么这些值为零。

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

       if let url = NSURL(string: self.restaurants[indexPath.row].restaurantImage), let data = NSData(contentsOfURL: url), let image =  UIImage(data: data) {

            dispatch_async(dispatch_get_main_queue()) { () -> Void in
                    cell.restaurantImage.image = image
            }
        }
    })

推荐阅读