Noa*_*ino 1 uiimageview uiimage ios firebase swift
我从我的 firebase 数据库中获取一个 url 来下载图像,并且在主线程中的 cell.articleImage.image = UIImage(data: data) 上收到此错误,它没有崩溃,但不会返回图像,有谁知道会出什么问题?
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
DispatchQueue.global().async {
let v = UIView(frame: .zero)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "articleCell") as? articlesCell else {
return UITableViewCell()}
let article = articleArray[indexPath.row]
let url = URL(string: article.imageURL)!
DispatchQueue.global().async {
do{
let data = try Data(contentsOf: url)
DispatchQueue.global().sync {
cell.articleImage.image = UIImage(data: data)
}
} catch {
}
}
// cell.articleImage.sd_setImage(with: url, placeholderImage: UIImage(named: "issaimage"))
cell.configureCell(title: article.ArticleTitle, author: article.author, date: article.date)
return cell
}
Run Code Online (Sandbox Code Playgroud)
您在全局而不是主队列中执行此操作,因此请更改此设置
DispatchQueue.global().sync {
cell.articleImage.image = UIImage(data: data)
}
Run Code Online (Sandbox Code Playgroud)
到
DispatchQueue.main.sync {
cell.articleImage.image = UIImage(data: data)
}
Run Code Online (Sandbox Code Playgroud)
//
同样,SDWebImage是最好的,因为使用这种方法,图像将在每次滚动时下载
//
也在你的 viewDidLoad
DispatchQueue.global().async { // should be in main queue
let v = UIView(frame: .zero) // whatever UI is here
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4739 次 |
最近记录: |