通过 Collection View Cell Swift 中的 segue 将图像传递给另一个视图控制器

Posted

技术标签:

【中文标题】通过 Collection View Cell Swift 中的 segue 将图像传递给另一个视图控制器【英文标题】:Pass Image to another view controller through segue from Collection View Cell Swift 【发布时间】:2017-12-12 08:11:55 【问题描述】:

我正在将图像上传到UIImage 并将其保存到集合视图单元格中。现在我想在 collection view 中点击图片并通过 Segue 将图片传递给另一个 ViewController。谁能帮忙..

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UICollectionViewDataSource,UICollectionViewDelegate 

    @IBOutlet weak var myImageView: UIImageView!
    var Photos = [UIImage]()

    @IBOutlet weak var ucvMyCollectionView: UICollectionView!
    override func viewDidLoad() 
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        ucvMyCollectionView.dataSource = self
        ucvMyCollectionView.delegate = self
        let nib = UINib(nibName: "myCollectionViewCell", bundle: nil)
        ucvMyCollectionView.register(nib, forCellWithReuseIdentifier: "myCollectionViewCell")
    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    

    @IBAction func selectPhotoButtonTapped(_ sender: Any) 
        let myPickerController = UIImagePickerController()
        myPickerController.delegate = self;
        myPickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary

        self.present(myPickerController, animated: true, completion: nil)
    
    internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])

    
        //myImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
        let newImage = info[UIImagePickerControllerOriginalImage] as! UIImage
        Photos.append(newImage)
        NSLog("%d",Photos.count)
        ucvMyCollectionView.reloadData()
        self.dismiss(animated: true, completion: nil)

    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
        return Photos.count
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCollectionViewCell", for: indexPath as IndexPath) as! myCollectionViewCell
        NSLog("ImageCount %d",Photos.count)
        cell.imgCollectionView.image = Photos[indexPath.row]

        return cell
    

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize 
        return CGSize(width: 200, height: 200)
    



【问题讨论】:

didSelectRow委托方法中获取单元格,然后从单元格中获取图像并将其传递给下一个视图控制器。 【参考方案1】:

UICollectionViewDelegate 有一个可选的方法实现func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)。 当用户选择(点击)collectionView 的其中一个单元格时,将调用此方法。不要忘记设置它的委托,如yourCollectionView.delegate = self

您必须在此方法中处理您的代码。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 
    // This gives you the selected cell
    let selectedCell = collectionView.cellForItem(at: indexPath)
    //
    // cast it to your cell type
    let cellInYourType = selectedCell as! YourCollectionViewCellType
    //
    // get your image
    let image = cellInYourType.imgCollectionView.image
    //
    // Create your viewController instance
    let yourVC = YourViewController();
    //
    // your data is passed like this
    yourVC.image = image;
    self.present(yourVC, animated: true, completion: nil)

如果您的 viewController 是在 Storyboard 中设置的,您可以使用这种方式初始化和设置图像。

let yourViewController = UIStoryBoard(name: "storyboardname", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerID") as! YourViewController
yourViewController.image = theImage;
self.present(yourViewController, animated: true, completion: nil)

【讨论】:

以上是关于通过 Collection View Cell Swift 中的 segue 将图像传递给另一个视图控制器的主要内容,如果未能解决你的问题,请参考以下文章

ScrollTo indexPath 在 Collection View Cell 内的 Collection View (Nested CollectionView)

iOS开发之collection view 的视图裁剪问题

什么定义了 UITableViewCell 中 Collection View Cell 的大小?

将 UIImage 分配给 Collection View Cell

iOS Collection View Change Cell content on didSelectItemAt indexPath

SWIFT-如果 indexPatih 为 0,如何删除 Collection View Cell?