尝试解包UIImage时意外发现nil

时间:2018-08-16 10:46:38

标签: swift

由于某种原因,当我尝试初始化图像时,结果为nil。 该图像保存在Assets文件夹中,并且名称正确。会导致它为零的原因是什么?

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == Storyboard.groupDetailsVC {
        if let vc = segue.destination as? GroupDetailsViewController {

            vc.groupImageView.image =  UIImage(named: "icon-checkbox")
            //it outputs error 

        }
    }

}

enter image description here

1 个答案:

答案 0 :(得分:2)

问题不在于Vc加载之前访问imageView的图像中

vc.groupImageView.image =  UIImage(named: "icon-checkbox")

//

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == Storyboard.groupDetailsVC {
        if let vc = segue.destination as? GroupDetailsViewController {
            vc.sendedImage =  UIImage(named: "icon-checkbox")

        }
    }

}

//

class GroupDetailsViewController:UIViewController {
  @IBOutlet weak var groupImageView: UIImageView!
  var sendedImage:UIImage!
  override func viewDidLoad() {
    super.viewDidLoad()
    self.groupImageView = sendedImage

  }
}