简体   繁体   English

如何将带有 imagepickercontroller 的照片作为缩略图放入 collectionView 单元格

[英]how to get a photo with imagepickercontroller into the collectionView cell as thumbnail

I try to create a code to pick photos from photo album and show it on the screen as thumbnail in the collection view cells.我尝试创建一个代码来从相册中挑选照片并将其显示在屏幕上作为集合视图单元格中的缩略图。 I will pick one photo per a button click and new photo will appear next to previous thumbnail.每次单击按钮,我都会选择一张照片,新照片将出现在以前的缩略图旁边。 CollectionView will have single line with vertical scroll. CollectionView 将有单行垂直滚动。 I struggle to transfer the image from pickercontroller didFinishPickingMediaWithInf method into the collectionView cellForItemAt method.我很难将图像从pickercontroller didFinishPickingMediaWithInf 方法转移到collectionView cellForItemAt 方法中。 I have found out a solution by using a dummy UIImageV but it only shows the first photo not the others.我通过使用虚拟 UIImageV 找到了解决方案,但它只显示第一张照片而不显示其他照片。 I don't get any crash.我没有任何崩溃。 could you please help me how to correct the code to upload photos I selected from photo album into the collection view cells.您能否帮助我更正将我从相册中选择的照片上传到收藏视图单元格的代码。 Please see the code below:请看下面的代码:

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UIImagePickerControllerDelegate,UINavigationControllerDelegate {

    public let imagePicker = UIImagePickerController()
    
    @IBOutlet weak var dummyImageView: UIImageView!
    
    private let reuseIdentifier = "myViewCell"
    
    var myImage = [UIImage]()
    
    @IBOutlet var collectionView: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        dummyImageView.isHidden = true
        collectionView.dataSource = self
        collectionView.delegate = self
                
    }
        
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return myImage.count    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
       let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier , for: indexPath) as! MyViewCell
        
        cell.backgroundColor = UIColor.blue
        
        let imageView = cell.myImage
        myImage.append(dummyImageView.image!)
        imageView!.image = myImage[indexPath.row]
        
        return cell
        
    }

    @IBAction func imageButtonPressed(_ sender: Any) {
        
        imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary
        imagePicker.allowsEditing = false
         
        self.present(imagePicker, animated: true, completion: nil)
      
       
        }
      

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
    
        if  let image = info[.originalImage] as? UIImage{
            print("last value",myImage.count)
            
            dummyImageView.image = image
            myImage.append(dummyImageView.image!)
            
            collectionView.reloadData()
           
        
        } else {
            print("error to load")
        }
               
       dismiss(animated: true, completion: nil)    
    }
}

The issue is not obvious so I had to recreate it on my machine.这个问题并不明显,所以我不得不在我的机器上重新创建它。 Actually myImage array is getting loaded twice in below functions.实际上 myImage 数组在下面的函数中被加载了两次。

  1. didFinishPickingMediaWithInfo didFinishPickingMediaWithInfo
  2. cellForItemAt cellForItemAt

This causes the data from collection view to get added to the myImage array.这会导致来自集合视图的数据被添加到 myImage 数组中。 Removing the below assignment in cellForItemAt, solves the issue.删除 cellForItemAt 中的以下分配,解决了该问题。 // myImage.append(dummyImageView.image!) // myImage.append(dummyImageView.image!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从imagePickerController拾取照片后,如何更改标题单元格中的imageView? - How to change imageView in header cell after pick a photo from imagePickerController? 如何使用 Firebase 将新照片上传到 collectionView 单元格? - How would I upload new photo into collectionView cell using Firebase? 通过电子邮件发送使用imagePickerController拍摄的照片 - Email a photo taken with imagePickerController imagePickerController选择照片时出错 - imagePickerController error selecting a Photo 如何用从ImagePickerController拍摄的照片替换UIButton图像 - How to replace a UIButton image with a photo taken from ImagePickerController 如何使用Swift在iOS中获得方形裁剪的照片缩略图 - How to get square cropped photo thumbnail in ios using swift 如何获取由imagePickerController添加的图片的路径 - How to get path of picture added by imagePickerController 如何通过生成pdf的缩略图将图像预加载到CollectionView中? - How to preload the images in CollectionView by generating a Thumbnail of the pdf? Swift CollectionView获取第一个单元格 - Swift CollectionView get first cell 如何在swift2中获取当前collectionview的单元格大小? - How to get the current collectionview cell size in swift2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM