我正在尝试执行将图像数组传递给新视图控制器的 segue,但 segue 是在将图像添加到数组之前执行的 - 我相信这是因为我用来将 url 转换为图像的 URLSession 需要时间。
将图像添加到数组后,我将如何让 Segue 执行?
我的代码,其中网址变成了图像:
// When selection is selected
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let overallArray = displayArray[indexPath.section]
// Add Image 1 to imageArrayToBeSent
if let imageUrl = overallArray.fullImage1 {
let url = URL(string: imageUrl)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print(error!)
return
}
DispatchQueue.main.async {
self.imageArrayToBeSent.append(UIImage(data: data!)!)
}
}.resume()
}
// stringToBeSent = overallArray.fullImage1!
performSegue(withIdentifier: "openNewViewController", sender: self)
}
我称之为Segue的代码:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let info = segue.destination as! NewViewController
info.imageArray = self.imageArrayToBeSent
print(self.imageArrayToBeSent)
}
在回调中执行此操作
DispatchQueue.main.async {
self.imageArrayToBeSent.append(UIImage(data: data!)!)
self.performSegue(withIdentifier: "openNewViewController", sender: self)
}