ios - Swift在第一个collectionview单元格中添加相机预览
问题描述
我想在第一个 collectionCell 中放置一个相机预览(就像 Telegram 一样)(而其他单元格中充满了来自画廊的照片)。当用户点击第一个单元格时,我需要显示一个摄像头。但是,当我点击单元格时,我在 xCode 中没有任何信息就崩溃了。
let cameraPicker: UIImagePickerController = {
let picker = UIImagePickerController()
picker.sourceType = .camera
picker.cameraDevice = .front
picker.showsCameraControls = false
return picker
}()
override func viewDidLoad() {
super.viewDidLoad()
imageCollectionView.delegate = self
imageCollectionView.dataSource = self
if UIImagePickerController.isCameraDeviceAvailable(.front) {
cameraPicker.delegate = self
//my cell size
let screenSize = CGSize(width: 80, height: 80)
let cameraAspectRatio = CGFloat(4.0 / 3.0)
let cameraImageHeight = screenSize.width * cameraAspectRatio
let scale = screenSize.height / 40
cameraPicker.cameraViewTransform = CGAffineTransform(translationX: 0, y: (screenSize.height - cameraImageHeight)/2)
cameraPicker.cameraViewTransform = cameraPicker.cameraViewTransform.scaledBy(x: scale, y: scale)
cameraPicker.view.frame = CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height)
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
guard let cell = cell as? PSTBaseImageCollectionViewCell else { return }
cell.imageView.subviews.forEach{$0.removeFromSuperview()}
cell.imageView.image = nil
if indexPath.row == 0 {
} else {
let asset = allPhotos?.object(at: indexPath.row - 1)
cell.imageView.fetchImageFromAsset(asset: asset!, contentMode: .aspectFill, targetSize: CGSize(width: asset!.pixelWidth, height: asset!.pixelHeight))
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.row == 0 {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
cameraPicker.showsCameraControls = true
present(cameraPicker, animated: true)
}
}
}
在这种情况下,如果我点击第一个单元格,相机会正常显示,但是当我在单元格中添加相机预览时,相机视图不再显示
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
guard let cell = cell as? PSTBaseImageCollectionViewCell else { return }
cell.imageView.subviews.forEach{$0.removeFromSuperview()}
cell.imageView.image = nil
if indexPath.row == 0 {
cell.imageView.addSubview(cameraPicker.view)
} else {
let asset = allPhotos?.object(at: indexPath.row - 1)
cell.imageView.fetchImageFromAsset(asset: asset!, contentMode: .aspectFill, targetSize: CGSize(width: asset!.pixelWidth, height: asset!.pixelHeight))
}
}
我错过了什么?
解决方案
你为什么使用 willDisplay 来设置你的单元格?
“ 讨论 集合视图在将单元格添加到其内容之前调用此方法。使用这种方法来检测单元格的添加,而不是监控单元格本身以查看它何时出现。”</p>
使用这个:https ://developer.apple.com/documentation/uikit/uicollectionviewdatasource/1618029-collectionview
推荐阅读
- exception - System.IO.FileLoadException:在 RichEditBox 文档中引发异常
- c# - 如何在 Azure Devops 中使用服务帐户模拟用户
- json - 使用 jq 命令行处理器时 JSON 格式的问题
- sql - 偏移量不适用于 Count(*) 作为 order by
- javascript - 从上传的图片生成 DataURI 并添加到 PDF (JsPDF)
- docker - 什么是 Docker 中的悬空图像/它们如何创建/如何删除它们
- gnuplot - 如何在编码iso_8859_1中写希腊后者?
- c++ - 将 8bpp 位图转换为 32bpp 位图 C++
- c# - 为新的 WEB Api 应用程序搭建控制器时出错
- c# - 无论如何在该方法中的等待调用完成之前执行该方法的返回语句