繁体   English   中英

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

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

我正在将图像上传到UIImage并将其保存到“收藏UIImage视图”单元格中。 现在,我想在集合视图中单击图像,然后将图像通过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)
    }


}

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

如果在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM