我有一个提供评论的表格视图。每个评论都包含一个标签和一个UIImage
,用于显示作者个人资料图片。加载表时,图像的显示方式没有任何模式。有时他们装得很好。其他时候,占位符图像出现,图像永远不会加载。如果我滚动,行为会有所不同。我正在使用AlamofireImage
。
经过一些研究后,我发现这篇文章的建议是打电话
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")
}
==== EDIT ====
添加其他信息希望它会有所帮助:
我已尝试按照以下建议设置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%的时间我在视图或滚动之间来回移动,占位符图像显示在实际图像的位置。事实上。占位符通常在第一次加载时出现。
=== EDIT#2 ===
我正在试图跟随Ashley Mills的第二个建议。我检查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")
}
此时我已尝试过一切。请帮忙。
答案 0 :(得分:0)
UITableViewController
重新使用单元格。为了防止旧图像重新出现在尚未完全加载的单元格中,请在加载前将单元格的图像设置为nil
。
cell.profileImage.image = nil
let myURL = comment.profileImageURL
if comment.profileImageURL != nil {
[...]
} else {
cell.profileImage.image = UIImage(named: "Profile Image")
}
答案 1 :(得分:0)
如果从屏幕滚动,则可以重复使用在图像中显示的每个单元格。使用throwException
将获取图像,并在收到响应时显示图像,但此时可能会重复使用该单元格,并尝试在不同的URL上显示图像。
您可以通过多种方式处理此问题,但首先要做的是向您的单元格添加<bean id="myException" class="package.path.of.customException">
<constructor-arg index="0" value="Custom message"/>
<property name="someIntProperty" value="10"/>
<property name="anotherIntProperty" value="#{null}"/>
</bean>
<camelContext ...>
<onException>
<exception>org.apache.camel.http.common.HttpOperationFailedException.HttpOperationFailedException</exception>
<throwException ref="myException" />
</onException>
</camelContext>
属性,并在那里处理下载,而不是在视图控制器中处理(您可能会发现所有af_setImage
属性Comment
有助于此 - 它确保您无法从视图控制器更新单元格的用户界面)
1)重复使用单元格时取消下载请求...
IBOutlet
这样可以防止返回上一个网址的响应,但这意味着图片不会被下载。
2)检查响应所针对的URL是您期望的URL。
fileprivate
这样即使图像没有显示,图像仍会被下载。