使用UITableView Cell的Swift 4 AlamofireImage

use*_*331 10 uitableview ios swift alamofire alamofireimage

我正在使用AlamofireImages尝试从服务器下载图像并将其显示在我的表格视图单元格中,这是我得到的,但我的图像没有出现,只有textLabel和detailTextLabel

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("http://xxxxxxx.com/uploads/" + (self.array[indexPath.row]["cover"] as! String)).responseImage { response in
            if let image = response.result.value {
                cell.imageView?.image = image
            }
        }

        return cell
    }
Run Code Online (Sandbox Code Playgroud)

小智 2

尝试这个:

 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!)
                }
Run Code Online (Sandbox Code Playgroud)

这对我来说效果很好。希望这会有所帮助。