Ios ImageView只能从主线程使用
我正在从firebase数据库获取一个url,用于下载图像,并且在主线程中的cell.articleImage.image=UIImage(data:data)上出现此错误,它没有崩溃,但不会返回图像,有人知道可能出了什么问题吗Ios ImageView只能从主线程使用,ios,swift,firebase,uiimageview,uiimage,Ios,Swift,Firebase,Uiimageview,Uiimage,我正在从firebase数据库获取一个url,用于下载图像,并且在主线程中的cell.articleImage.image=UIImage(data:data)上出现此错误,它没有崩溃,但不会返回图像,有人知道可能出了什么问题吗 override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self
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
}
您在全局队列中而不是在主队列中执行此操作,因此请更改此设置
DispatchQueue.global().sync {
cell.articleImage.image = UIImage(data: data)
}
到
//
同样,这是最好的,因为这种方法的图像将下载每个滚动
//
同样在您的viewDidLoad
DispatchQueue.global().async { // should be in main queue
let v = UIView(frame: .zero) // whatever UI is here
}
您在全局队列中而不是在主队列中执行此操作,因此请更改此设置
DispatchQueue.global().sync {
cell.articleImage.image = UIImage(data: data)
}
到
//
同样,这是最好的,因为这种方法的图像将下载每个滚动
//
同样在您的viewDidLoad
DispatchQueue.global().async { // should be in main queue
let v = UIView(frame: .zero) // whatever UI is here
}
它只是返回空白图像然后你的url有问题,复制一个internet图像url,硬编码,然后检查url有问题,谢谢你给我的所有帮助它只是返回空白图像然后你的url有问题,复制一个internet图像url,硬编码,然后检查url有问题,谢谢你给我的帮助