执行segue

时间:2017-01-11 16:38:49

标签: ios swift swift3 grand-central-dispatch

我试图通过执行/准备segue将图像添加到位于另一个视图控制器中的另一个imageView.image,但是当我尝试在bg线程中执行变量然后跳转到主线程来更新它,它不起作用。但是,如果我删除调度的东西,那么它的工作原理。这是代码:

 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()
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我会建议KingFisher这样的事情。这需要所有垃圾并将其转化为:

 . . .
let cell = sender as! UICollectionViewCell
let url = URL(string: self.images[indexPath.row].rawURL)
vc.imageView.kf.setImage(with: url)
 . . .
但是,是的。 @dlbuckley是对的。如果你不做像KingFisher这样的事情,请不要在prepareForSegue中做那些事情。