简体   繁体   English

通过从Collection View Cell Swift通过segue将Image传递到另一个视图控制器

[英]Pass Image to another view controller through segue from Collection View Cell Swift

I am uploading an image to UIImage and Saving it into Collection View cell. 我正在将图像上传到UIImage并将其保存到“收藏UIImage视图”单元格中。 Now I want to click the image in collection view and pass the image to another ViewController through Segue. 现在,我想在集合视图中单击图像,然后将图像通过Segue传递给另一个ViewController。 can anyone please help.. 谁能帮忙..

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


}

UICollectionViewDelegate has an optional method implementation func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) . UICollectionViewDelegate有一个可选的方法实现func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) When one of the cells of collectionView is selected(tapped) by user, this method is called. 当用户选择(点击)collectionView的单元格之一时,将调用此方法。 Don't forget to set it's delegate like yourCollectionView.delegate = self 不要忘记将其设置为yourCollectionView.delegate = self

You will have to handle your code inside this method. 您将必须在此方法中处理您的代码。

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

If your viewController is setup in Storyboard, you can initialize and set the image using this way. 如果在Storyboard中设置了viewController,则可以使用这种方式初始化和设置图像。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将图像从集合视图控制器快速传递到另一个视图控制器 - swift pass images from collection view controller to another view controller Swift 2.1-如何将集合视图单元格的索引行传递给另一个视图控制器 - Swift 2.1 - How to pass index row of collection view cell to another view controller 如何根据“收藏”标识从“收藏”视图单元中选择到特定视图控制器 - How to Segue to a specific view controller depending on the Segue identity, from a Collection view cell 自定义单元格中的选定行不会将完整数据从segue传递到另一个视图控制器 - selected row in custom cell doesn't pass complete data from segue to another view controller 如何将集合视图单元格的indexPath传递给另一个视图控制器 - How to pass a collection view cell's indexPath to another view controller 我正在尝试通过“准备转场”将数据从一个视图控制器传递到另一个视图控制器,但它崩溃了 - I am trying to pass data from one View Controller to another in Swift via 'prepare for segue' but it crashes 使用Firebase将图像从一个视图控制器传递到另一个(swift) - pass an image from one view controller to another (swift) with Firebase 将数据从集合视图单元格按钮传递到另一个视图控制器(Swift) - Passing data from a collection view cell button to another view controller (Swift) Swift 4:将数据从集合视图单元格传递到另一个没有故事板的视图控制器 - Swift 4: Passing data from a collection view cell into another view controller without storyboards 将图像视图从集合视图传递到新的视图控制器 - Pass image view from collection view to a new view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM