// // ListViewController.swift // CapFinder // // Created by User on 22.02.18. // Copyright © 2018 User. All rights reserved. // import UIKit import SwiftyDropbox class ImageSelector: UIViewController { // MARK: - Constants /// The number of items per row private let itemsPerRow: CGFloat = 3 override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .portrait } override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation { return .portrait } override var shouldAutorotate: Bool { return false } // MARK: - CollectionView private var images = [UIImage?]() var cap: Cap! @IBOutlet weak var collection: UICollectionView! // MARK: - Life cycle override func viewDidLoad() { super.viewDidLoad() collection.dataSource = self collection.delegate = self } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) downloadImages() } // MARK: Image download private func downloadImages() { images = [UIImage?](repeating: nil, count: cap.count) event("\(cap.count) images for cap \(cap.id)") if let image = cap.image { self.images[0] = image self.collection.reloadItems(at: [IndexPath(row: 0, section: 0)]) } else { cap.downloadImage { mainImage in self.images[0] = mainImage self.collection.reloadItems(at: [IndexPath(row: 0, section: 0)]) } } guard cap.count > 0 else { return } for number in 1.. Int { return 1 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return images.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell( withReuseIdentifier: "Image", for: indexPath) as! ImageCell cell.capView.image = images[indexPath.row] return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { selectedImage(nr: indexPath.row) navigationController?.popViewController(animated: true) } } // MARK: - UICollectionViewDelegateFlowLayout extension ImageSelector : UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let widthPerItem = collectionView.frame.width / itemsPerRow return CGSize(width: widthPerItem, height: widthPerItem) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 0 } } extension ImageSelector: Logger { static let logToken = "[ImageSelector]" }