UICollectionViewCell 不显示从 UIImagePickerController 选取的图像
UICollectionViewCell not showing images picked from UIImagePickerController
有人可以在这里看到任何阻止单元格 "photoInTheCellnow" 显示来自图像选择器
的 UIImage 值的错误吗
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) { }
}
}
这是自定义单元格class
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:indexPath
中的集合视图单元格。
cell.imageView.image = photosInTheCellNow[indexPath.row]
您还需要在选择图像后更新 collectionView
。您已将图像添加到 photosInTheCellNow
属性,但您没有让 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)
}
有人可以在这里看到任何阻止单元格 "photoInTheCellnow" 显示来自图像选择器
的 UIImage 值的错误吗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) { }
}
}
这是自定义单元格class
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:indexPath
中的集合视图单元格。
cell.imageView.image = photosInTheCellNow[indexPath.row]
您还需要在选择图像后更新 collectionView
。您已将图像添加到 photosInTheCellNow
属性,但您没有让 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)
}