![](/img/trans.png)
[英]Swift UItableview with AlamofireImage crash when scrolling fast
[英]Swift 4 AlamofireImage with UITableView Cell
我正在使用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
}
當然,您的圖像不會為您顯示。 當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的庫,它將為您處理異步加載圖像,從而使單元格表平穩運行。
嘗試這個:
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!)
}
這對我來說很好。.希望這會有所幫助。
這對我有幫助:
首先創建此功能可幫助下拉圖像。 如果要提取多個圖像,則可能需要創建一個新的swift文件並將其設為靜態,或者只是在需要的文件中調用它。 這樣,您可以引用它而不是重寫代碼:
static func loadImage(_ imageView: UIImageView,_ urlString: String) {
let imgURL: URL = URL(string: urlString)!
URLSession.shared.dataTask(with: imgURL) { (data, respond, error) in
guard let data = data, error == nil else { return}
DispatchQueue.main.async (execute: {
imageView.image = UIImage(data: data)
})
}.resume()
}
然后拉下信息:
if let logoName = variable.logo {
StaticFileName.loadImage(cell.imgVariableInCell, "\(logoName)")
}
return cell
}
那你很好。
您需要在主線程中運行此代碼。
DispatchQueue.main.async {
cell.imageView?.image = image
}
您可以使用SDWebImage
首先,您需要導入
import SDWebImage
然后,您可以使用此方法
let imgUrl = URL(string:"your image URL string")
cell.imageView?.sd_setImage(with: imgUrl, placeholderImage: UIImage(named: "propic"))
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.
})
}
您的代碼是正確的。 我認為傳輸安全性會通過http阻止您的圖像。 將此添加到您的info.plist文件中
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>example.com</key> //put your domain name here
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
您還可以使用alamofire的圖片庫。 只需添加pod alamofireImage。
首先,您必須檢查一下,如果得到響應,您是否得到了正確的響應,而不是編寫如下代碼
Alamofire.request(imageUrl!, method: .get).response { response in
if let image = UIImage(data: response.data!) {
cell. imageView?.image = image
}
}
您應該將鏈接作為變量傳遞給單元格中的變量,並重寫UITableViewCell中的awakefromnib函數。 在awakeFrom筆尖內部,您調用alamofire請求以將圖像加載到單元中,通過這種方式,您不會在每次重用單元時都調用查詢。
所以
// CellCode
import Alamofire
...
var imageLink:String?
override func awakeFromNib() {
super.awakeFromNib()
Alamofire.request(imageLink).responseImage { response in
if let image = response.result.value {
self.imageView?.image = image
}
}
}
//cell configuration
cell.imageLink = "http://xxxxxxx.com/uploads/" + self.array[indexPath.row]["cover"] as! String)
Swift-5支持的代碼,可以完美運行
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tblView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
cell.lblName.text = arrData[indexPath.row]["name"] as? String
let imageUrl = arrData[indexPath.row]["imageUrl"] as? String
Alamofire.request(imageUrl!, method: .get).response { response in
guard let image = UIImage(data:response.data!) else {
// Handle error
return
}
let imageData = image.jpegData(compressionQuality: 1.0)
cell.imgCourses.image = UIImage(data : imageData!)
}
return cell
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.