首页 > 解决方案 > 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))
    }
}

我错过了什么?

标签: iosswiftcamerauicollectionview

解决方案


你为什么使用 willDisplay 来设置你的单元格?

https://developer.apple.com/documentation/uikit/uicollectionviewdelegate/1618087-collectionview?language=objc

“ 讨论 集合视图在将单元格添加到其内容之前调用此方法。使用这种方法来检测单元格的添加,而不是监控单元格本身以查看它何时出现。”</p>

使用这个:https ://developer.apple.com/documentation/uikit/uicollectionviewdatasource/1618029-collectionview


推荐阅读