【问题标题】:swift pass images from collection view controller to another view controller将图像从集合视图控制器快速传递到另一个视图控制器
【发布时间】:2017-06-16 04:02:28
【问题描述】:

我已经从 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
        }
    }

【问题讨论】:

  • *它不是完整的代码,但我分享了最重要的几行
  • 如果没有看到所有代码,很难知道如何回答这个问题,在摘录中提供的变量 photoThumbnail 从未设置并且始终为零。

标签: ios swift uicollectionview segue


【解决方案1】:

尝试改成这段代码:

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

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

【讨论】:

    【解决方案2】:

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

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

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

     override func viewDidLoad() {
            super.viewDidLoad()
    
    
            if let getImage = myImage
            {
            myLabel.text = labelText
            enlargedPhoto.image = getImage 
            }
        }
    

    【讨论】:

      【解决方案3】:

      第一个视图控制器

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

      【讨论】:

        【解决方案4】:
        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 
            }
        
        }
        

        【讨论】:

        • 但是我已经下载了图像,如果我再次下载它,那将是糟糕的内存管理。不过谢谢
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-22
        相关资源
        最近更新 更多