【问题标题】:Using UIImagePickerController with a UICollectionView将 UIImagePickerController 与 UICollectionView 一起使用
【发布时间】:2020-02-26 18:57:23
【问题描述】:

我正在编写一段代码,逻辑如下:

- 每次点击按钮时,都会打开一个新视图,并允许用户从手机图库中选择一张图片。 - 所选图像应加载并显示在水平滚动视图中。

我面临的问题是选择者在选择图像时拾取器擦除罚款,但是图像不加载在拾取器解雇的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
    }



}

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

【问题讨论】:

    标签: ios swift uicollectionview uiscrollview scrollview


    【解决方案1】:

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

    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()
            }
        }
    
    

    【讨论】:

    • 感谢您的快速回答^^
    猜你喜欢
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-19
    相关资源
    最近更新 更多