将图像从 collectionview 传递到 VC

Pass image from collectionview to VC

我试了很多方法都没用

ViewController 1 个有:Collectionview > Cell > 单元格内的图像

ViewController 2要显示VC 1

中的图像

当您点击单元格时,它会自动将您推送到 VC 2

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "popup" {

        let viewController: friendpopup = segue.destination as! friendpopup
        let indexPath = sender as! NSIndexPath
        let nicknamex = self.nicknameArray[indexPath.row]
        let usernamex = self.userArray[indexPath.row]
        let photox = self.friendsphotos[indexPath.row] // the photos PFFiles i think


        viewController.snick = nicknamex
        viewController.suser = usernamex
        viewController.sphoto = // ????
}

昵称和用户工作正常,只有我无法显示的图像。

我试过当你点击单元格时它会将图像发送到 var 但不起作用

var photovar:UIImage!

didSelectItemAt(self.photovar = cell.profilepic.image) 然后在 prepareSegue( viewcontroller.sphoto = self.photovar)

无法正常工作,谁能帮我解决这个问题以显示图像?谢谢

稍微收紧你的代码....

第一个VC

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "popup" {
        if let vc = segue.destination as? friendpopup {
            let indexPath = sender as! NSIndexPath
            vc.snick = self.nicknameArray[indexPath.row]
            vc.suser = self.userArray[indexPath.row]
            vc.sphoto = self.friendsphotos[indexPath.row]
        }
    }
}

第二个VC:

// making assumptions on variable types
var snick:String!
var suser:String!
var sphoto:UIImage!

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // do work here
    if sphoto != nil {
        imageView.image = sPhoto
    } else {
        // set a default image here
    }
}

其中大部分与您的代码相似,但最后要注意 - 如果前 2 个属性正确出现,请检查第一个 VC 您是否正确拉出图像。