2017-12-12 2 views
0

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


} 
+0

Holen Sie sich Zelle in 'didSelectRow' Delegate-Methode, dann Bild aus der Zelle abrufen und es an die nächste Ansicht Controller übergeben. –

Antwort

1

UICollectionViewDelegate verfügt über eine optionale Methode Implementierung func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath). Wenn eine der Zellen von collectionView vom Benutzer ausgewählt (getappt) wird, wird diese Methode aufgerufen. Vergessen Sie nicht, seinen Delegierten wie yourCollectionView.delegate = self

setzen Sie müssen Ihren Code innerhalb dieser Methode behandeln.

Wenn Ihr viewController im Storyboard eingerichtet ist, können Sie das Bild auf diese Weise initialisieren und einstellen.

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