我正在使用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
}发布于 2018-05-21 15:14:26
试试这个:
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!)
}这对我来说很好..希望这能对你有所帮助。
发布于 2018-05-28 15:55:00
当然,您的图像不会显示给您。因为当Alamofire开始下载你的图像时,它会在后台线程中进行处理。但你是在主线程或UI线程中工作,所以你需要从后台切换到主线程,这将为你工作。
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
}
}
}如果你认为这对你来说很难做到这一点,我建议你使用这个叫做Kingfisher的库,它将为你处理异步加载图像,使你的单元格的表顺利运行。
发布于 2018-05-22 13:01:59
使用AlamofireImages
if let url = URL(string: "your url") {
cell.imageView?.af_setImage(withURL:url, placeholderImage: nil, filter: nil, imageTransition: .crossDissolve(0.2), runImageTransitionIfCached: false, completion: {response in
// do stuff when is downloaded completely.
})
}https://stackoverflow.com/questions/50440980
复制相似问题