将 UIImagePickerController 与 UICollectionView 一起使用

Using UIImagePickerController with a UICollectionView

我正在编写一段具有以下逻辑的代码:

- On every tap on a button, a new view opens and allows the user to choose an image from the phone's gallery.
- The chosen image should be loaded and displayed in an horizontal scroll view. 

我面临的问题是,选择图像时,采摘器会忽略罚款,但是该图像不会在Picker diviver上加载Uicollectionview内部(占位符的图像保留,并且不会被选定的图像替换)) .我的控制台没有发生崩溃或错误。感谢您的帮助!

这是我的这个功能的视图控制器:

public class ImagePickerViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource {

    var mediaArray = [UIImage]()

    @IBAction func galleryButtonTapped(_ sender: Any) {
        let imagePickerController = UIImagePickerController()
        imagePickerController.delegate = self

        let actionSheet = UIAlertController(title: "Gallery image", message: "Choose an image from your gallery", preferredStyle: .actionSheet)

        actionSheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (action: UIAlertAction) in
            imagePickerController.sourceType = .photoLibrary
            self.present(imagePickerController,animated: true, completion: nil )

        }))

        actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

        self.present(actionSheet, animated: true,completion: nil)

    }

    public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage

        mediaArray.append(selectedImage)


        picker.dismiss(animated: true, completion: nil)
    }


    public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }



    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return mediaArray.count
    }

    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MediaCollectionViewCell", for: indexPath) as! MediaCollectionViewCell

        cell.mediaContent.image = mediaArray[indexPath.row]

        return cell
    }



}

这是我的水平滚动视图的一部分

选择图像后重新加载您的收藏视图。

public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage

        mediaArray.append(selectedImage)


        picker.dismiss(animated: true) {
            self.collectionView.reloadData()
        }
    }