Swift 3 - 如何使用“AVFCapturePhotoOutput” - 无法识别的原始格式BGRA

时间:2017-03-12 22:24:08

标签: ios swift swift3 avfoundation

我正在使用Swift 3并为iPhone 7开发。

我有自定义相机视图,我尝试拍照。我正在尝试显示已拍摄照片的UIImage但我收到以下错误:

  

[AVCapturePhotoOutput   DNGPhotoDataRepresentationForRawSampleBuffer:previewPhotoSampleBuffer:]   无法识别的原始格式BGRA'

This answer告诉我,iPhone不支持kCVPixelFormatType_24RGB(出于某种原因)所以我不知道该怎么做。非常感谢任何帮助。

func shutterButtonPressed() {

    // apparently kCVPixelFormatType_24RGB is not supported for iPhone?
    let photoPixelFormatType = kCVPixelFormatType_32BGRA

    // using uncompressed format
    let settings = AVCapturePhotoSettings(
                    format: [kCVPixelBufferPixelFormatTypeKey as String : photoPixelFormatType])

    // thumbnail
    let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first!
    let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
                         kCVPixelBufferWidthKey as String: 160,
                         kCVPixelBufferHeightKey as String: 160,
                         ]
    settings.previewPhotoFormat = previewFormat

    // get the actual video feed and take a photo from that feed
    session_output.capturePhoto(with: settings, delegate: self)


}

func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {

    if let error = error {
            print(error.localizedDescription)
        }

        // ERROR ON THIS LINE
        if let sampleBuffer = photoSampleBuffer, let previewBuffer = previewPhotoSampleBuffer, let dataImage = AVCapturePhotoOutput.dngPhotoDataRepresentation(forRawSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) {

            // this is the image that the user has taken!
            let takenImage : UIImage = UIImage(data: dataImage)!
        } else {
            print("Error setting up photo capture")
        }

1 个答案:

答案 0 :(得分:1)

我认为您可能使用了错误的AVCapturePhotoOutput委托方法。你能试试

吗?
captureOutput:didFinishProcessingRawPhotoSampleBuffer:previewPhotoSampleBuffer:resolvedSettings:bracketSettings:error?

您还应该使用AVCapturePhotoSettings(rawPixelFormatType:, processedFormat:)进行设置。