swift将图像从集合视图控制器传递到另一个视图控制器

时间:2017-06-16 04:02:29

标签: ios swift uicollectionview segue

我从firebase下载了一组用户照片并将其视为集合视图(像Instagram一样) 但我试图放大照片使用segue点击另一个视图控制器,但它无法正常工作。 关于我的代码的任何想法:

class ProfileViewController: UIViewController{
    var photoThumbnail: UIImage! 
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection_cell", for: indexPath) as! ProfileCollectionViewCell
        cell.imageCollection.downloadImage2(from: currPosts[indexPath.row].photoUrl)
        cell.imageCollection.image = photoThumbnail
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        performSegue(withIdentifier: "photoViewSegue", sender: nil)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        var destViewController : EnlargePhotoViewController = segue.destination as! EnlargePhotoViewController
        destViewController.labelText = "test"  //**** this works
        destViewController.myImage = photoThumbnail //**** this doesnt work 
    }

}

,集合视图单元格为:

class ProfileCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var imageCollection: UIImageView!

}

最后是目标viewController:

class EnlargePhotoViewController: UIViewController {

        @IBOutlet weak var myLabel: UILabel!
        @IBOutlet weak var enlargedPhoto: UIImageView!

        var labelText = String()
        var myImage: UIImage!

        override func viewDidLoad() {
            super.viewDidLoad()

           myLabel.text = labelText
            enlargedPhoto.image = myImage
        }
    }

4 个答案:

答案 0 :(得分:2)

尝试更改为此代码:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath) as! ProfileCollectionViewCell

    photoThumbnail = cell.imageCollection.image
    performSegue(withIdentifier: "photoViewSegue", sender: nil)
}

答案 1 :(得分:1)

您唯一的目标是获取所选单元格的图像,这可以更有效地完成。所有你要做的就是读/写单元格imageView属性,photoThumbnail永远不会设置所以总是为零,只需修改这个

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection_cell", for: indexPath) as! ProfileCollectionViewCell
    cell.imageCollection.downloadImage2(from: currPosts[indexPath.row].photoUrl)
    //cell.imageCollection.image = photoThumbnail <-- No need of this
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {


  // get the correct cell on select 
   if let cell = collectionView.cellForItem(at: indexPath) as! ProfileCollectionViewCell
   {
    photoThumbnail =  cell.imageCollection.image <-- finally assign the imageview to image
    performSegue(withIdentifier: "photoViewSegue", sender: self)
   }
}

为了更安全地检索图像,请使用

 override func viewDidLoad() {
        super.viewDidLoad()


        if let getImage = myImage
        {
        myLabel.text = labelText
        enlargedPhoto.image = getImage 
        }
    }

答案 2 :(得分:0)

class ProfileViewController: UIViewController{
    var photoThumbnail: UIImage!
    var selectedPhotoURL: NSUrl!


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection_cell", for: indexPath) as! ProfileCollectionViewCell
        cell.imageCollection.downloadImage2(from: currPosts[indexPath.row].photoUrl)
        cell.imageCollection.image = photoThumbnail
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectedPhotoURL = currPosts[indexPath.row].photoUrl
        performSegue(withIdentifier: "photoViewSegue", sender: nil)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        var destViewController : EnlargePhotoViewController = segue.destination as! EnlargePhotoViewController
        destViewController.labelText = "test"  //**** this works
        //make image from selected url or pass url and download it in EnlargePhotoViewController
        let downlodedImage = yourMethodToDownload(selectedPhotoURL) 
        destViewController.myImage = downlodedImage //**** this will work work 
    }

}

答案 3 :(得分:0)

第一视图控制器

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.row == 0
        {
            let objstory = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
        objstory.strd = imageView.image
        _ = self.navigationController?.pushViewController(objstory, animated: true)

        }
    }

<强> SecondViewController

 var strd:UIImage!
@IBOutlet weak var imgprof: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()

        imgprof.image = strd


        // Do any additional setup after loading the view.
    }