繁体   English   中英

Swift 在第一个 collectionview 单元格中添加相机预览

[英]Swift Add camera preview in first collectionview cell

我想在第一个 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 来设置单元格?

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

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

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

我可能为时已晚,但我找到了解决方案。 我在我的应用程序中做了同样的事情,但我添加了一个 UIView 并将其 isUserInteractionEnabled 设置为 false 而不是 imageview

在 cellForRowAt 中:

if indexPath.row == 0 {
    cell.preview.addSubview(cameraPicker.view)
    cameraPicker.view.frame = cell.imageView.bounds
    cell.preview.isUserInteractionEnabled = false
} else {
    let asset = allPhotos?.object(at: indexPath.row - 1)
    cell.imageView.fetchImageFromAsset(asset: asset!, contentMode: .aspectFill, targetSize: CGSize(width: asset!.pixelWidth, height: asset!.pixelHeight))
}

我在单元格中仍然有一个 imageView,但这只是为了显示带有我的应用程序徽标的渐变色调(alpha 设置为 0.3),使其看起来像一个叠加层

编辑:我也意识到你从来没有为相机预览设置框架

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM