0
UIImage
に画像をアップロードしていて、コレクションビューのセルに保存しています。今私は、コレクションビューで画像をクリックし、Segueを通して別のViewControllerに画像を渡したいと思います。誰もが..コレクションビュー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)
}
}
セルを 'didSelectRow'デリゲートメソッドで取得し、セルからイメージを取得して次のビューコントローラに渡します。 –