将图像从collectionview传递给VC

时间:2016-11-11 19:44:47

标签: swift swift3 cell collectionview

我尝试了很多方法,但我无法让它工作

ViewController 1具有:Collectionview>细胞>细胞内的图像

ViewController 2想要显示VC 1中的图像

当你点击单元格时,它有segue将你推送到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)

不是,任何人都可以帮我解决这个问题以显示图片吗?感谢

1 个答案:

答案 0 :(得分:0)

稍微加强你的代码......

第一次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
    }
}

大部分内容与您的代码类似,但最后一个注意事项 - 如果前两个属性正确发生,请检查第一个正确拉出图像的VC。