UICollectionView 中没有显示相机拍摄的图像,如何显示从 UICollectionView 拍摄的图像?
Camera captured image is not showing in UICollectionVIew, how to display images captured from UICollectionView?
我一直在尝试在 UICollectionView
中显示和存储相机捕获的图像,但不知何故 UICollectionView
无法显示来自相机的图像,我在 [=21] 中检查了 self.images.count
=]
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
它正在返回拍摄的图像数量,但不知何故,UICollectionView
是空的..
我做到了
self.imgCollectionView.reloadData()
和
连接了 dataSource
和 delegate
..
里面
ViewController_1.swift
let cameraAlert = UIAlertController(title: "", message: "upload", preferredStyle: .actionSheet)
let cameraUpload = UIAlertAction.init(title: "upload from camera", style: .default, handler: {_ in
self.uploadFromCamera()
})
func uploadFromCamera() {
if UIImagePickerController.isSourceTypeAvailable( .camera) {
let cameraPicker = UIImagePickerController()
cameraPicker.delegate = self
cameraPicker.sourceType = .camera
cameraPicker.cameraCaptureMode = .photo
self.navigationController?.present(cameraPicker, animated: true, completion: nil )
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let pickedIMG = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
if let vc = self.storyboard?.instantiateViewController(identifier: "CapturedPhotoViewController") as? CapturedPhotoViewController {
vc.images.append(pickedIMG)
self.navigationController?.pushViewController(vc, animated: true)
}
}
dismiss(animated: true, completion: nil)
}
CollectionViewController.swift
class CapturedPhotoViewController: UIViewController {
@IBOutlet weak var imgCollectionView: UICollectionView!
var images: [UIImage] = []
override func viewDidLoad() {
super.viewDidLoad()
self.imgCollectionView.reloadData()
}
}
extension CapturedPhotoViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CapturedPhotoCell", for: indexPath) as! CapturedPhotoCell
let imgContent = self.images[indexPath.row]
cell.configure(image: imgContent)
return cell
}
}
extension CapturedPhotoViewController: UICollectionViewDelegate {
}
extension CapturedPhotoViewController: UICollectionViewDelegateFlowLayout {
}
class CapturedPhotoCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
func configure(image: UIImage) {
self.imageView.image = image
}
}
我的猜测是,当图像数组为空时,您正在集合视图控制器中调用 reloadData()
。当您的 CapturedPhotoViewController
被实例化时 (viewDidLoad
),您的 self.imgCollectionView.reloadData()
部分代码被调用,此时图像数组为空。
无需进行太多更改的最简单修复方法是在 VC 出现后调用 reloadData()
if let vc = self.storyboard?.instantiateViewController(identifier: "CapturedPhotoViewController") as? CapturedPhotoViewController {
vc.images.append(pickedIMG)
self.navigationController?.pushViewController(vc, animated: true)
vc.imgCollectionView.reloadData()
}
我找到了解决方案..现在可以使用了..
@available(iOS 13.0, *)
extension ViewController: UIImagePickerControllerDelegate & UINavigationControllerDelegate {
func showImagePickerOptions() {
let cameraAlert = UIAlertController(title: "", message: "upload", preferredStyle: .actionSheet)
let cameraUpload = UIAlertAction.init(title: "upload from camera", style: .default, handler: {_ in
self.showPickerController(sourceType: .camera)
})
let libraryUpload = UIAlertAction.init(title: "upload from library", style: .default, handler: {_ in
self.showPickerController(sourceType: .photoLibrary)
})
cameraAlert.addActions(actions: [cameraUpload, libraryUpload])
self.navigationController?.present(cameraAlert, animated: true, completion: nil)
}
func showPickerController(sourceType: UIImagePickerController.SourceType) {
let pickerController: UIImagePickerController = UIImagePickerController()
pickerController.allowsEditing = true
pickerController.delegate = self
pickerController.sourceType = sourceType
self.navigationController?.present(pickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: {
if let vc = self.storyboard?.instantiateViewController(identifier: "CapturedPhotoViewController") as? CapturedPhotoViewController {
vc.title = "image Selection"
vc.images = self.images
DDLogDebug("CapturedImageVC image count:\(vc.images.count)")
self.navigationController!.pushViewController(vc, animated: true)
}
})
guard let image = info[.editedImage] as? UIImage else {
DDLogDebug("No image.. . \nSomething went wrong in < didFinishPickingMediaWithInfo > ")
return
}
self.images.append(image)
// print imageSize and imageCount to check if it is nil or not.. .
print(image.size)
print(self.images.count)
}
}
CollectionViewVC:
class CapturedPhotoViewController: UIViewController {
@IBOutlet weak var imgCollectionView: UICollectionView!
var images: [UIImage]!
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension CapturedPhotoViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.images?.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CapturedPhotoCell", for: indexPath) as! CapturedPhotoCell
guard let imageContent = self.images?[indexPath.row] else {
DDLogDebug("Image content is nil.. .")
return cell
}
cell.configure(image: imageContent)
return cell
}
}
extension CapturedPhotoViewController: UICollectionViewDelegate {
}
extension CapturedPhotoViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let screenSize: CGRect = UIScreen.main.bounds
let screenWidth = screenSize.width
return CGSize(width: (screenWidth/4) - 8, height: (screenWidth/4) - 8)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 2
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 2
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 7, left: 7, bottom: 7, right: 7)
}
}
class CapturedPhotoCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
func configure(image: UIImage) {
self.imageView.image = image
}
}
我一直在尝试在 UICollectionView
中显示和存储相机捕获的图像,但不知何故 UICollectionView
无法显示来自相机的图像,我在 [=21] 中检查了 self.images.count
=]
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
它正在返回拍摄的图像数量,但不知何故,UICollectionView
是空的..
我做到了
self.imgCollectionView.reloadData()
和
连接了 dataSource
和 delegate
..
里面
ViewController_1.swift
let cameraAlert = UIAlertController(title: "", message: "upload", preferredStyle: .actionSheet)
let cameraUpload = UIAlertAction.init(title: "upload from camera", style: .default, handler: {_ in
self.uploadFromCamera()
})
func uploadFromCamera() {
if UIImagePickerController.isSourceTypeAvailable( .camera) {
let cameraPicker = UIImagePickerController()
cameraPicker.delegate = self
cameraPicker.sourceType = .camera
cameraPicker.cameraCaptureMode = .photo
self.navigationController?.present(cameraPicker, animated: true, completion: nil )
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let pickedIMG = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
if let vc = self.storyboard?.instantiateViewController(identifier: "CapturedPhotoViewController") as? CapturedPhotoViewController {
vc.images.append(pickedIMG)
self.navigationController?.pushViewController(vc, animated: true)
}
}
dismiss(animated: true, completion: nil)
}
CollectionViewController.swift
class CapturedPhotoViewController: UIViewController {
@IBOutlet weak var imgCollectionView: UICollectionView!
var images: [UIImage] = []
override func viewDidLoad() {
super.viewDidLoad()
self.imgCollectionView.reloadData()
}
}
extension CapturedPhotoViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CapturedPhotoCell", for: indexPath) as! CapturedPhotoCell
let imgContent = self.images[indexPath.row]
cell.configure(image: imgContent)
return cell
}
}
extension CapturedPhotoViewController: UICollectionViewDelegate {
}
extension CapturedPhotoViewController: UICollectionViewDelegateFlowLayout {
}
class CapturedPhotoCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
func configure(image: UIImage) {
self.imageView.image = image
}
}
我的猜测是,当图像数组为空时,您正在集合视图控制器中调用 reloadData()
。当您的 CapturedPhotoViewController
被实例化时 (viewDidLoad
),您的 self.imgCollectionView.reloadData()
部分代码被调用,此时图像数组为空。
无需进行太多更改的最简单修复方法是在 VC 出现后调用 reloadData()
if let vc = self.storyboard?.instantiateViewController(identifier: "CapturedPhotoViewController") as? CapturedPhotoViewController {
vc.images.append(pickedIMG)
self.navigationController?.pushViewController(vc, animated: true)
vc.imgCollectionView.reloadData()
}
我找到了解决方案..现在可以使用了..
@available(iOS 13.0, *)
extension ViewController: UIImagePickerControllerDelegate & UINavigationControllerDelegate {
func showImagePickerOptions() {
let cameraAlert = UIAlertController(title: "", message: "upload", preferredStyle: .actionSheet)
let cameraUpload = UIAlertAction.init(title: "upload from camera", style: .default, handler: {_ in
self.showPickerController(sourceType: .camera)
})
let libraryUpload = UIAlertAction.init(title: "upload from library", style: .default, handler: {_ in
self.showPickerController(sourceType: .photoLibrary)
})
cameraAlert.addActions(actions: [cameraUpload, libraryUpload])
self.navigationController?.present(cameraAlert, animated: true, completion: nil)
}
func showPickerController(sourceType: UIImagePickerController.SourceType) {
let pickerController: UIImagePickerController = UIImagePickerController()
pickerController.allowsEditing = true
pickerController.delegate = self
pickerController.sourceType = sourceType
self.navigationController?.present(pickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: {
if let vc = self.storyboard?.instantiateViewController(identifier: "CapturedPhotoViewController") as? CapturedPhotoViewController {
vc.title = "image Selection"
vc.images = self.images
DDLogDebug("CapturedImageVC image count:\(vc.images.count)")
self.navigationController!.pushViewController(vc, animated: true)
}
})
guard let image = info[.editedImage] as? UIImage else {
DDLogDebug("No image.. . \nSomething went wrong in < didFinishPickingMediaWithInfo > ")
return
}
self.images.append(image)
// print imageSize and imageCount to check if it is nil or not.. .
print(image.size)
print(self.images.count)
}
}
CollectionViewVC:
class CapturedPhotoViewController: UIViewController {
@IBOutlet weak var imgCollectionView: UICollectionView!
var images: [UIImage]!
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension CapturedPhotoViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.images?.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CapturedPhotoCell", for: indexPath) as! CapturedPhotoCell
guard let imageContent = self.images?[indexPath.row] else {
DDLogDebug("Image content is nil.. .")
return cell
}
cell.configure(image: imageContent)
return cell
}
}
extension CapturedPhotoViewController: UICollectionViewDelegate {
}
extension CapturedPhotoViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let screenSize: CGRect = UIScreen.main.bounds
let screenWidth = screenSize.width
return CGSize(width: (screenWidth/4) - 8, height: (screenWidth/4) - 8)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 2
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 2
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 7, left: 7, bottom: 7, right: 7)
}
}
class CapturedPhotoCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
func configure(image: UIImage) {
self.imageView.image = image
}
}