setup of collection view in horizontal format with an array of images in different cells : I have created a collection view and I want to set five cells with images in them. We can capture the images from camera. I want the cells to be kept like in the horizontal format.


Here in the first image I add the imagePickerController to open the camera.

[a cell with the did select action of opening camera][1]


[each image captured should set in each cell like this!!!][2]


[the 5th image should settle in place of the take photo cell!!!][3]

Also, these above images are just a design of how I wanna set the collection view to look in the end.


  [1]: https://i.stack.imgur.com/aoaiT.png
  [2]: https://i.stack.imgur.com/ymCPC.png
  [3]: https://i.stack.imgur.com/Jw4Qh.png

`import UIKit
import AVKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    
    //
    let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard
    var items: [UIImage] = [
        UIImage(named: "user-icon")!,
        UIImage(named: "lock-icon")!,
        UIImage(named: "search.png")!
    ]
    var imgPicker = UIImagePickerController()
    //
    //    // MARK: - UICollectionViewDataSource protocol
    //
    //    // tell the collection view how many cells to make
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.items.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! CollectionViewCell
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1)
        {
            print(self.items[indexPath.row])
        }
        cell.imgPicker1.image = items[indexPath.row]
        
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        return CGSize(width: 80, height: 80)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
    }
    
    //MARK:- UI ImagePicker Delegate Methods
    
    
    func openCamera(){
        if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerController.SourceType.camera)) {
            let authStatus = AVCaptureDevice.authorizationStatus(for: .video)
            switch authStatus {
            case .authorized:
                showCamera()
            case .denied:
                let alert = UIAlertController(title: NSLocalizedString("Alert!", comment: "") + " \n" + NSLocalizedString("Please enable the Permission for Camera", comment: ""), message: NSLocalizedString("So that Group Icon can be set.", comment: ""), preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .default) { action in
                    self.openSettingToEnableNotification()
                })
                self.present(alert, animated: true)
            case .notDetermined:
                AVCaptureDevice.requestAccess(for: AVMediaType.video,
                                              completionHandler: { (granted:Bool) -> Void in
                                                if granted {
                                                    print("access granted", terminator: "")
                                                }
                                                else {
                                                    print("access denied", terminator: "")}
                })
            default:
                openSettingToEnableNotification()
            }
        } else {
            let alertController = UIAlertController(title: NSLocalizedString("Error", comment: ""), message: NSLocalizedString("Device has no camera", comment: ""), preferredStyle: .alert)
            let defaultAction = UIAlertAction(title: NSLocalizedString("Ok", comment: ""), style: .default, handler: { (alert) in
            })
            alertController.addAction(defaultAction)
            present(alertController, animated: true, completion: nil)
        }
    }
    func openSettingToEnableNotification(){
        UIApplication.shared.open(URL(string:UIApplication.openSettingsURLString)!)
    }
    func showCamera()           {if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerController.SourceType.camera))
    {
        imgPicker.delegate = self
        imgPicker.sourceType = .camera
        //imgPicker.allowsEditing = true
        self.present(imgPicker, animated: true, completion: nil)
    }
    else
        {
            let alert  = UIAlertController(title: NSLocalizedString("Warning", comment: ""), message: NSLocalizedString("You don't have camera", comment: ""), preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }
    // MARK: - UICollectionViewDelegate protocol
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        openCamera()
        
    }
}`


