【问题标题】:How to get [UIImage] from camera (AVCaptureSession)如何从相机中获取 [UIImage] (AVCaptureSession)
【发布时间】:2021-06-29 07:16:37
【问题描述】:

我正在尝试将从相机获取的 UIImage 添加到 [UIImage],但是当我使用以下代码时,相机将在添加 12 帧后停止

var temp1:CGImage?

extension VideoCapture: AVCaptureVideoDataOutputSampleBufferDelegate {


    public func captureOutput(_ output: AVCaptureOutput,
                              didOutput sampleBuffer: CMSampleBuffer,
                              from connection: AVCaptureConnection) {
        
       // print(1)
        guard let delegate = self.delegate else { return }
//        print(sampleBuffer.imageBuffer)
        if let pixelBuffer = sampleBuffer.imageBuffer {
            // Attempt to lock the image buffer to gain access to its memory.
            guard CVPixelBufferLockBaseAddress(pixelBuffer, .readOnly) == kCVReturnSuccess
                else {
                    return
            }
            // Create Core Graphics image placeholder.
            var image: CGImage?
            
            
            // Create a Core Graphics bitmap image from the pixel buffer.
            VTCreateCGImageFromCVPixelBuffer(pixelBuffer, options: nil, imageOut: &image)
            DispatchQueue.main.async {
                if image != nil{
                    self.temp.append(image)
                    print(self.temp.count)
                }
                delegate.videoCapture(self, didCaptureFrame: image)
            }

//
            
        }
    }

【问题讨论】:

    标签: swift uiimage avcapturesession


    【解决方案1】:

    如果你使用UIImagePickerController ,你可以使用UIImagePickerControllerDelegate开发

    例如

    
    var temp1:UIImage?
    
    extension VideoCapture: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
        
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            
            if let image = info[UIImagePickerController.InfoKey(rawValue: "UIImagePickerControllerEditedImage")] as? UIImage {
                
                // modified Image
                temp1 = image.convertToGrayScale()
                
            } else if let image = info[UIImagePickerController.InfoKey(rawValue: "UIImagePickerControllerOriginalImage")] as? UIImage {
                
                // original Image
                temp1 = image.convertToGrayScale()
            }
           
            picker.dismiss(animated: true, completion: nil)
            
        }
    

    【讨论】:

      猜你喜欢
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 2013-06-10
      • 2017-03-26
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多