I'm trying to add an image to another imageView.image which is located in another view controller by performing/preparing for segue, however when I try to do the variables in the bg thread and then hop to the main thread to update it, it doesn't work. But if I remove the dispatch thing, then it works. Here's the code:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
SwiftSpinner.show(progress: 0.2, title: "Loading full image, please wait...")
let vc = segue.destination as! PhotoViewController
let cell = sender as! UICollectionViewCell
DispatchQueue.global(qos: .background).async {
if let indexPath = self.collectionView?.indexPath(for: cell) {
let url = URL(string: self.images[indexPath.row].rawURL)
let imageData = NSData(contentsOf: url!)
let image = UIImage(data: imageData as! Data)
DispatchQueue.main.async {
vc.image = image!
vc.mainImage = image!
vc.photographerName = self.images[(indexPath.row)].photographerName
SwiftSpinner.hide()
}
}
}
}
I would recommend KingFisher for things like this. It takes all that junk and turns it into:
. . .
let cell = sender as! UICollectionViewCell
let url = URL(string: self.images[indexPath.row].rawURL)
vc.imageView.kf.setImage(with: url)
. . .
But yeah. @dlbuckley is right. Don't do that stuff in prepareForSegue if you don't do something like KingFisher.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.