Swift UICollectionViewCell不显示从UIImagePickerController拾取的图像
有人能在这里看到阻止单元格“PhotoInCellNow”从图像选择器显示UIImage值的任何错误吗Swift UICollectionViewCell不显示从UIImagePickerController拾取的图像,swift,uiimagepickercontroller,Swift,Uiimagepickercontroller,有人能在这里看到阻止单元格“PhotoInCellNow”从图像选择器显示UIImage值的任何错误吗 import UIKit import Photos import PhotosUI class FirstViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UICollectionViewDataSource, UICollectionView
import UIKit
import Photos
import PhotosUI
class FirstViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var collectionView: UICollectionView!
var photosInTheCellNow = [UIImage]()
var imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
imagePicker.delegate = self
}
@IBAction func openLibrary(_ sender: UIBarButtonItem) {
let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
switch photoAuthorizationStatus {
case .authorized: print("Access is granted by user")
case .notDetermined:
PHPhotoLibrary.requestAuthorization({ (newStatus) in print("status is \(newStatus)"); if newStatus == PHAuthorizationStatus.authorized { print("success") } })
case .restricted: print("User do not have access to photo album.")
case .denied: print("User has denied the permission.")
}
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
photosInTheCellNow.append(image)
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return photosInTheCellNow.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotosCollectionViewCell", for: indexPath) as! PhotosCollectionViewCell
return cell
}
func register(_ cellClass: PhotosCollectionViewCell, forCellWithReuseIdentifier identifier: String) { }
}
}
这是自定义单元类
import UIKit
class PhotosCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func prepareForReuse() {
super.prepareForReuse()
}
}
你失踪了
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotosCollectionViewCell", for: indexPath) as! PhotosCollectionViewCell
cell.imageView.image = photosInTheCellNow[indexPath.row]
return cell
}
正如@RaziTiwana所提到的,您需要将图像分配给
cellForItemAt:indepath
中的集合视图单元格
cell.imageView.image = photosInTheCellNow[indexPath.row]
您还需要在拾取图像后更新collectionView
。您已将图像添加到PhotosinCellNow
属性中,但未让collectionView
知道数据源已更改。您可以通过调用collectionView.reloadData()
重新加载整个collectionView
,或者只插入最后一行,如图所示
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
photosInTheCellNow.append(image)
let indexPath = IndexPath(item: photosInTheCellNow.count-1, section: 0)
collectionView.insertItems(at: [indexPath])
picker.dismiss(animated: true, completion: nil)
}
不工作了。我仍然有这个错误:error Domain=PlugInKit Code=13“query cancelled”UserInfo={NSLocalizedDescription=query cancelled这对我来说是个好问题,我不认为否决投票是合理的。这是一个基本问题(这完全可以!)但它有所有重要的部分。一个清晰的代码示例,对问题的解释,以及对期望结果的解释(虽然在本例中是隐含的)。补充@Alexander的观点,而一次否决票可能(可以说)不合理的,我们不会去处理他们的事情。考虑孤立的下票,就像雨天——他们是一个微不足道的麻烦,但只是你的一天。没有义务选民解释他们的选票-他们是匿名的长期政策。如果你在这里做一些好的职位,你很快会赚取更多的U。p而不是向下。@halfer和系统甚至解释了这一点。问题向下投票给5个代表,而向下投票只损失2个代表。因此所有
n
问题都可以50%向上投票,50%向下投票(导致净0分),你仍然会有n
声誉这篇文章的另一个反馈是,代码没有任何格式就被粘贴到其中,并且必须由一名志愿者编辑修复。我想这就是投票失败的原因,尽管这完全是一个意见问题,但人们可以推测,选民认为这是正确的tified。我建议在提交问题之前使用预览窗格。