【问题标题】:Instance member 'fileDataRepresentation' cannot be used on type 'AVCapturePhoto'; did you mean to use a value of this type instead?实例成员“fileDataRepresentation”不能用于“AVCapturePhoto”类型;你的意思是使用这种类型的值吗?
【发布时间】:2021-09-01 16:25:41
【问题描述】:

Swift 给出了无法为 AVCapturePhoto 创建 fileDataRepresentation 的错误。打印“照片”,返回照片的元数据。如何将图像转换为文件字节并最终转换为 base64?到目前为止,我已经尝试了很多方法,但都依赖于获取文件字节。感谢您提前回复!

   @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
          settings.isHighResolutionPhotoEnabled = false
          sessionOutput.capturePhoto(with: settings, delegate: self)
        
    }

    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Swift.Error?) {
        let imageData = AVCapturePhoto.fileDataRepresentation()

【问题讨论】:

    标签: swift avfoundation capture avcapture avcapturephotooutput


    【解决方案1】:

    AVCapturePhoto.fileDataRepresentationinstance 方法,而不是 class 方法。

    您必须像这样在 AVCapturePhoto 的实例上调用它 -

    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Swift.Error?) {
        let imageData = photo.fileDataRepresentation()
    }
    

    【讨论】:

    • 此方法并将其转换为 base64 图像正在创建损坏的图像。有没有办法解决这个问题?代码: let uiImage = UIImage(data: imageData!) let pngImage = uiImage?.pngData() let base64Data = pngImage!.base64EncodedString()
    • 你应该可以像let base64String = photo.fileDataRepresentation()?.base64EncodedString()一样直接将这些数据转换成base64字符串。这条路线没必要走image > pngData > base64
    猜你喜欢
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多