代码之家  ›  专栏  ›  技术社区  ›  Chris

Swift-从UICollectionViewCell获取UIImage

  •  0
  • Chris  · 技术社区  · 5 年前

    collectionView 每个 cell UIImage 和一个空的 UIButton

    class ImageCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
    
        @IBOutlet weak var imageCollectionView: UICollectionView!
    
        let images: [UIImage] = [
            UIImage(named: "beerImage")!,
            UIImage(named: "christmasImage")!,
            UIImage(named: "goalImage")!,
            UIImage(named: "travelImage")!,
            UIImage(named: "rollerImage")!,
            UIImage(named: "giftImage")!,
            UIImage(named: "shirtImage")!,
            UIImage(named: "dressImage")!,
    
        ]
    
        let columnLayout = FlowLayout(
            itemSize: CGSize(width: 150, height: 150),
            minimumInteritemSpacing: 30,
            minimumLineSpacing: 10,
            sectionInset: UIEdgeInsets(top: 20, left: 20, bottom: 10, right: 20)
        )
    
        var colViewWidth: CGFloat = 0.0
    
        override func viewDidLoad() {
    
            self.imageCollectionView.collectionViewLayout = columnLayout
    
            super.viewDidLoad()
    
            imageCollectionView.dataSource = self
            imageCollectionView.delegate = self
        }
    
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return images.count
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell
    
            cell.imageView.image = images[indexPath.item]
            cell.layer.cornerRadius = 2
            return cell
        }
    
        @IBAction func imageButtonTapped(_ sender: Any) {
            print("tapped")
        }
    
    
        @IBAction func closeButtonTapped(_ sender: Any) {
            let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let HomeViewController = storyBoard.instantiateViewController(withIdentifier: "HomeVC") as! ExampleViewController
                    self.present(HomeViewController, animated: false, completion: nil)
        }
    }
    

    更新

    最后,我想把这个图像传递给另一个ViewController。我尝试了这种方式,但图片没有显示在其他ViewController中:

    var tappedImage = UIImage()
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        tappedImage = images[indexPath.row]
    }
    
    var showPopUpView = true
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        print(showPopUpView)
        var vc = segue.destination as! ExampleViewController
        vc.pickedImage = self.tappedImage
        vc.ShowPopUpView = self.showPopUpView
    
    }
    

    视图控制器B

        var pickedImage = UIImage()
    
    override func viewDidLoad() {
            super.viewDidLoad()
    
            imagePreview.image = pickedImage
    
    0 回复  |  直到 5 年前
        1
  •  3
  •   Community Neeleshkumar S    4 年前

    根据评论中的对话:

    1. 用户单击单元格
    2. 根据 indexPath
    3. 在临时变量中设置( tappedImage
    4. 表演环节
    5. 将临时映像传递到目标的临时映像( pickedImage
    6. 加载目标视图

    似乎您错过了其中一个步骤(可能是步骤3,因为您没有打电话) performSegue ...didSelectItemAt indexPath... 可能根本就没人打电话!试着在那里打印一些东西并检查它是否是真的)

    之后 目的地 View

    调试:

    点击图片 , images[indexPath.row] ,然后查看问题出在何处,以及在何处丢失选定的图像参考

    可能的问题

    你没有使用 表演格 ,可能你有 UIButton 所以这个按钮会阻止 cell 不会被选中。

        2
  •  3
  •   tgrable    5 年前

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        imageToPass = images[indexPath.row]
    }
    

    编辑: 至于将该图像传递给下一个视图控制器,一种可能的方法是为该图像的第一个和第二个视图控制器添加属性,在 didSelectItemAt 打电话,然后用 prepare(for segue: UIStoryboardSegue, sender: Any?)

    let imageToPass: UIImage?
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       if segue.identifier == "identifier name here" {
    
           // Get the new view controller using segue.destination.
           if let destVC = segue.destination as? SecondViewController {
               // Pass the selected object to the new view controller.
               destVC.image = imageToPass
           }
       }
    }
    
        3
  •  0
  •   Vinoth M    5 年前

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let image = images[indexPath.row]
        //Result image
    }
    

    (或)

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
       let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCollectionViewCell
    
       cell.imageView.image = images[indexPath.item]
       cell.layer.cornerRadius = 2
    
       //Replace your button
       cell.yourButton.tag = indexPath.row
       cell.yourButton.addTarget(self, action: #selector(self.imageButtonTapped(_:)), for: .touchUpInside)
    }
    

    按钮动作中

    func imageButtonTapped(_ sender: UIButton) {
       let image = images[sender.tag]
       //Result image
    }