Swift 4 AlamofireImage with UITableView Cell

后端 未结 10 3307
梦如初夏
梦如初夏 2021-02-20 12:56

I am using AlamofireImages to try to download an image from a server and display it in my table view cell, this is what I got, but my images does not appear, just the textLabel

10条回答
  •  梦如初夏
    2021-02-20 13:39

    Of course your image will not show for you. Cause when Alamofire starts downloading your image its process in background thread. But you are working in Main Thread or UI Thread so you need to swap from background to Main Thread it will work for you.

    Alamofire.request("http://xxxxxxx.com/uploads/" + (self.array[indexPath.row]["cover"] as! String)).responseImage { response in
           DispatchQueue.main.async {
                if let image = response.result.value {
                    cell.imageView?.image = image
                }
            }
    }
    

    If you think it is difficulty for you to do this i recommend you to use this library called Kingfisher it will handle asynchronous loading images for you that make your cell's table run in smoothly.

    Link: https://github.com/onevcat/Kingfisher

提交回复
热议问题