我在viewController中有一些默认图像,在用户操作后,它会加载外部图像并替换默认图像. 问题是,它加载图像但不刷新,除非我将viewController更改为其他 DispatchQueue.main.async { self.getDataFromUrl(th
问题是,它加载图像但不刷新,除非我将viewController更改为其他
DispatchQueue.main.async { self.getDataFromUrl(thumbURL, completion: { (data) in let image = UIImage(data: data!) self.cityImageView.image = image print("img refreshed") }) } func getDataFromUrl(_ url:String, completion: @escaping ((_ data: Data?) -> Void)) { URLSession.shared.dataTask(with: URL(string: url)!, completionHandler: { (data, response, error) in if let newData = data { completion(newData) } }) .resume() }
所以我得到一个打印出来的img刷新但在前端没有任何变化,我缺少什么?
你能在主线程上设置图像吗?DispatchQueue.main.async { self.getDataFromUrl(thumbURL, completion: { (data) in DispatchQueue.main.async { let image = UIImage(data: data!) self.cityImageView.image = image print("img refreshed") } }) }