我有一個呈現註釋的表視圖。每條評論都包含一個標籤和一個提供作者個人資料圖片的UIImage
。加載表格時圖像出現的方式沒有模式。有時他們加載罰款。其他時候,佔位符圖像出現,圖像永不加載。如果我滾動,行爲會有所不同。我正在使用AlamofireImage
。加載圖像時UITableViewCells中的零星行爲
經過一番研究,我發現這個帖子裏的建議是呼籲
self.tableView.beginUpdates()
self.tableView.endUpdates()
在AlamofireImage的完成處理程序。這根本沒有效果。
這裏是我的原代碼:
func tableView(_ tableView: UITableView, cellForRowAt.....{
...
let myURL = Comment.profileImageURL
if Comment.profileImageURL != nil {
profileImage.af_setImage(withURL: myURL!, placeholderImage: #imageLiteral(resourceName: "Profile Image"))
} else {
profileImage.image = UIImage(named: "Profile Image")
}
這裏是基於this question's recommendation更新(在頁面上最後的答案):
let myURL = comment.profileImageURL
if comment.profileImageURL != nil {
cell.profileImage.af_setImage(
withURL: myURL!,
placeholderImage: #imageLiteral(resourceName: "Profile Image"),
filter: nil,
imageTransition: UIImageView.ImageTransition.crossDissolve(0.5),
runImageTransitionIfCached: false) {
// Completion closure
response in
// Check if the image isn't already cached
if response.response != nil {
// Force the cell update
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
}
} else {
cell.profileImage.image = UIImage(named: "Profile Image")
}
==== ====編輯
添加額外的信息,希望它會有所幫助: 我試過按照下面的建議設置profileImage.image = nil
。我也取消了下面建議的prepareForReuse()
的下載無效。
調用圖像之前,我做的UIImageView
profileImage.layer.cornerRadius = profileImage.bounds.size.width/2
profileImage.clipsToBounds = true
profileImage.layer.borderWidth = 2
profileImage.layer.borderColor = UIColor(red:222/255.0, green:225/255.0, blue:227/255.0, alpha: 1.0).cgColor
最後的問題本身。大約50%的時間我在視圖或滾動之間來回移動,佔位符圖像出現在實際圖像的位置。事實上。佔位符經常出現在第一次加載。
===編輯#2 ===
我試圖按照阿什利米爾斯第二個建議。我檢查AlamofireImage完成處理程序中的圖像URL,結果很奇怪。我只是加載了表格視圖。如下圖所示裝精的所有圖片:
我打印出來的圖像URL時完成處理火災和它看起來罰款:
我在完成 可選的是(http://www.smarttapp.com/Portals/0/Users/018/18/18/sballmer.jpg) 我在完成 可選(http://www.smarttapp.com/Portals/0/Users/018/18/18/sballmer.jpg) 我在完成 可選(http://www.smarttapp.com/Portals/0/Users/011/11/11/Elon%20Musk.jpeg) 我在完成 可選(http://www.smarttapp.com/Portals/_default/Users/001/01/1/Martin%20Profile.jpg)
使用導航欄,我退了出來,然後重新和圖像的顯示的所有佔位符圖片:
打印出如下:
我已完成無 我已完成無 我已完成無
最後,這是圖像加載代碼並完成處理:我在這一點上都試過了
let myURL = Comment.profileImageURL
if Comment.profileImageURL != nil {
// profileImage.af_setImage(withURL: myURL!, placeholderImage: #imageLiteral(resourceName: "Profile Image"))
profileImage.af_setImage(
withURL: myURL!,
placeholderImage: #imageLiteral(resourceName: "Profile Image"),
filter: nil,
completion:{ image in
print("I'm in completion")
print(image.response?.url)
self.setNeedsLayout()
self.layoutIfNeeded()
})
} else {
profileImage.image = UIImage(named: "Profile Image")
}
。請幫忙。
the4kman,謝謝你的建議。我跑了測試,可悲的是這並沒有解決問題,雖然它似乎有改善的東西。當圖像確實可用時,我仍然可以獲取佔位符圖像(需要下載) –