我有一个通过this third party library制作的RSS源。滚动浏览tableview时遇到问题。每当我向下滚动标签文本更改但一段时间后图像更改。在短短的几秒钟内,我可以看到该行的前一张图像。我希望看到一个默认图像,如果它在后端加载。用于他们的演示得到这个懒加载停止在tableview中同步加载图像
代码是:
func loadImageSynchronouslyFromURLString(_ urlString: String) -> UIImage? {
if let url = URL(string: urlString) {
let request = NSMutableURLRequest(url: url)
request.timeoutInterval = 30.0
var response: URLResponse?
let error: NSErrorPointer? = nil
var data: Data?
do {
data = try NSURLConnection.sendSynchronousRequest(request as URLRequest, returning: &response)
} catch let error1 as NSError {
error??.pointee = error1
data = nil
}
if (data != nil) {
return UIImage(data: data!)
}
}
return nil
}
下面是从哪里该函数获取调用代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "FeedItemCell", for: indexPath) as UITableViewCell
let item = entries![(indexPath as NSIndexPath).row]
if let imageView = cell.viewWithTag(1) as? UIImageView {
if item.mainImage != nil {
imageView.image = item.mainImage
} else {
if item.imageURLsFromDescription == nil || item.imageURLsFromDescription?.count == 0 {
item.mainImage = UIImage(named: "roundedDefaultFeed")
imageView.image = item.mainImage
}
DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async(execute: {() -> Void in
for imageURLString in item.imageURLsFromDescription! {
if let image = self.loadImageSynchronouslyFromURLString(imageURLString) {
item.mainImage = image
DispatchQueue.main.async(execute: {() -> Void in
imageView.image = image
self.tableView.reloadRows(at: [indexPath], with: .automatic)
})
break;
}
}
})
}
}
return cell
}
请告诉我如何显示空白在滚动后加载图片。
使用'asyn'加载数据。在您的'imageView'中显示默认图像,直到您从服务器接收到数据。 –
使用[SDWebImage](https://github.com/rs/SDWebImage)第三方课程。 – Ronak
@the_dahiya_boy你可以举个例子吗? – Dia