gpt4 book ai didi

swift - 将图像从collectionview传递到VC

转载 作者:行者123 更新时间:2023-11-30 12:54:44 25 4
gpt4 key购买 nike

尝试了很多方法都无法实现

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)

没有醒来,任何人都可以帮我解决这个问题以显示图像吗?谢谢

最佳答案

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

第一个风险投资

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]
}
}
}

第二个风险投资:

// 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。

关于swift - 将图像从collectionview传递到VC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40555120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com