[英]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.