Created
September 16, 2017 13:14
-
-
Save Abhishek-NickelFox/2b0a3931407093ec4de55eb010a8680d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| class ViewController: UIViewController { | |
| @IBOutlet weak var imgCollectionView: UICollectionView! | |
| var cellItems: [CellModel] = [] | |
| var imagePicker: UIImagePickerController? | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| self.imgCollectionView.delegate = self | |
| self.imgCollectionView.dataSource = self | |
| self.imagePicker = UIImagePickerController() | |
| self.imagePicker?.sourceType = .photoLibrary | |
| self.imagePicker?.delegate = self | |
| } | |
| override func didReceiveMemoryWarning() { | |
| super.didReceiveMemoryWarning() | |
| } | |
| @IBAction func openPhotos(_ sender: Any) { | |
| self.present(self.imagePicker!, animated: true, completion: nil) | |
| } | |
| } | |
| extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { | |
| func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { | |
| let image = info[UIImagePickerControllerOriginalImage] as! UIImage | |
| let model = CellModel(image: image) | |
| self.cellItems.append(model) | |
| self.imgCollectionView.reloadData() | |
| picker.dismiss(animated: true, completion: nil) | |
| } | |
| func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { | |
| picker.dismiss(animated: true, completion: nil) | |
| } | |
| } | |
| extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { | |
| func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
| return self.cellItems.count | |
| } | |
| func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
| let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionCell", for: indexPath) as! ImageCollectionCell | |
| cell.item = self.cellItems[indexPath.row] | |
| return cell | |
| } | |
| func collectionView(_ collectionView: UICollectionView, | |
| layout collectionViewLayout: UICollectionViewLayout, | |
| sizeForItemAt indexPath: IndexPath) -> CGSize { | |
| let dimension = (self.view.frame.size.width - 40)/3 | |
| return CGSize(width: dimension, height: dimension) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment