Swift 如果您在设置中指定非零格式字典,您的委托必须响应选择器 captureOutput:didFinishProcessingPhoto

问题描述 投票:0回答:2

我下面使用的代码应该拍摄照片,然后将图像转换为 Base64,以便将其发送到服务器。该代码可以拍摄照片,将其转换为 Base64,并将其上传到我的服务器,但已停止工作。我尝试过使用其他堆栈溢出帖子来解决此问题,但它对我不起作用。感谢您提前回复!

错误

线程 1:异常:“***

-[AVCapturePhotoOutput capturePhotoWithSettings:delegate:]
如果您在设置中指定非
nil
格式字典,您的委托必须响应选择器
captureOutput:didFinishProcessingPhoto:error:
,或已弃用的
captureOutput:didFinishProcessingPhotoSampleBuffer:previewPhotoSampleBuffer:resolvedSettings:bracketSettings:error:

代码:

@IBAction func takePhotoButtonPressed(_ sender: Any) {
      let settings = AVCapturePhotoSettings()
      let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first!
      let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,
                           kCVPixelBufferWidthKey as String: 160,
                           kCVPixelBufferHeightKey as String: 160]
      settings.previewPhotoFormat = previewFormat
      sessionOutput.capturePhoto(with: settings, delegate: self)
}
        

func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
    let imageData = photo.fileDataRepresentation()
    let base64String = imageData?.base64EncodedString()
    print(base64String!)
}
swift xcode avfoundation avcapturesession ibaction
2个回答
2
投票

让我们分解一下错误消息:

  • 主题 1:异常:

    程序的线程 1(可能是主线程)抛出了 Objective C 异常。不是特别有趣/有洞察力。

  • -[AVCapturePhotoOutput capturePhotoWithSettings:delegate:]

    此语法描述了引发异常的 Objective-C 方法。该方法的选择器是

    capturePhotoWithSettings:delegate:
    ,属于
    AVCapturePhotoOutput
    类。
    -
    表示它是一个实例方法(其中
    +
    表示它是一个类方法)。

  • 如果您在设置中指定非零格式字典...`

    就是这种情况,您调用

    capturePhoto(with: settings, ...
    时使用的设置不是
    nil

  • 您的代表必须响应选择器

    captureOutput:didFinishProcessingPhoto:error:

    系统抱怨您传递了一个不响应选择器的委托

    captureOutput:didFinishProcessingPhoto:error:
    (在 Swift 中,该方法被导入为
    photoOutput(_:didFinishProcessingPhoto:error:)
    )。

    也就是说,您的委托没有定义任何具有该名称的方法。您决定将

    self
    (无论那是什么,如果没有上下文我就不知道)作为代表:
    capturePhoto(..., delegate: self)

    无论

    self
    是什么类型,它都已经符合
    AVCapturePhotoCaptureDelegate
    协议(否则永远不会编译)。但它没有实现这个方法,该方法在协议中是可选的,但在这种情况下是强制性的。

  • 或已弃用的

    captureOutput:didFinishProcessingPhotoSampleBuffer:previewPhotoSampleBuffer:resolvedSettings:bracketSettings:error:

    这只是告诉您,您可以通过实现

    captureOutput:didFinishProcessingPhoto:error:
    来解决此问题,而不是
    captureOutput:didFinishProcessingPhotoSampleBuffer:previewPhotoSampleBuffer:resolvedSettings:bracketSettings:error:
    ,但由于它已被弃用,您可能不应该使用它。

所以总而言之,无论

self
是什么类型,您都需要确保它实现了方法
photoOutput(_:didFinishProcessingPhoto:error:)


0
投票

我在同事的项目中遇到了这个问题。委托方法存在,但错误仍然存在。经过调查,我注意到该类被声明为

public
。最后通过声明委托方法
public
解决了这个问题:

public class SomePublicClass {
    // Some code here
}

extension SomePublicClass: AVCapturePhotoCaptureDelegate {
    public func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
        // Do something
    }
}

我希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.