[英]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 数组在下面的函数中被加载了两次。
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.