'AVCapturePhotoOutput'类型的值没有成员'captureStillImageAsynchronously'

时间:2018-05-04 05:46:56

标签: swift avfoundation

请帮我重构这段代码,它不起作用,我在互联网上搜索但没有找到任何解决方案。我不能自己重构它。

这一行:

imageOutput.captureStillImageAsynchronously(from: connection) { (sampleBuffer, error) -> Void in

抛出此错误:

  

'AVCapturePhotoOutput'类型的值没有成员'captureStillImageAsynchronously'

  imageOutput.captureStillImageAsynchronously(from: connection) { (sampleBuffer, error) -> Void in
    if (sampleBuffer == nil || error != nil) {
      DispatchQueue.main.async {
        completion(nil, error)
      }
      return
    }

    guard let data = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer!, previewPhotoSampleBuffer: nil)  else {
      DispatchQueue.main.async {
        completion(nil, StillImageError.noData)
      }
      return
    }

    guard let image = UIImage(data: data) else {
      DispatchQueue.main.async {
        completion(nil, StillImageError.noImage)
      }
      return
    }

    var saver = ImageSaver()

        .onSuccess { image, assetId in

            completion(assetId, nil)

        }
        .onFailure { error in

    }
    saver = saver.save(image, filter:  nil)

  }

1 个答案:

答案 0 :(得分:1)

captureStillImageAsynchronously(from:completionHandler:) AVCaptureStillImageOutput 的函数,而不是 AVCapturePhotoOutput

由于自iOS 10以来不推荐使用AVCaptureStillImageOutput,因此您应该使用AVCapturePhotoOutput,就像在代码中一样。它具有相同用例的函数:capturePhoto(with:delegate:)

有关AVCapturePhotoOutput使用的更多信息,请check out this question