我正在将图片上传到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)
}
}
答案 0 :(得分:2)
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)