Ich lade ein Bild auf und speichern Sie es in Collection View Zelle. Jetzt möchte ich auf das Bild in der Sammlungsansicht klicken und das Bild über Segue an einen anderen ViewController übergeben. helfen kann jemand bitte ..Übergeben Sie Bild zu einem anderen View-Controller über den Übergang von Collection View Cell Swift
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)
}
}
Holen Sie sich Zelle in 'didSelectRow' Delegate-Methode, dann Bild aus der Zelle abrufen und es an die nächste Ansicht Controller übergeben. –