无法转换类型'(_) - >的值()'到期望参数类型'((Bool,错误?) - > Void)?'

时间:2018-03-19 14:58:25

标签: ios iphone swift swift3 ios9

请,我需要帮助是问题,我更改语法Swift 3 for swift 4现在我有很多问题来识别我的所有错误。错误它在函数savePhoto的最后一行。,completionHandler:{ _ in

func takePhoto(_ previewLayer: AVCaptureVideoPreviewLayer, location: CLLocation?, completion: (() -> Void)? = nil) {
    guard let connection = stillImageOutput?.connection(with: AVMediaType.video) else { return }

    connection.videoOrientation = Helper.videoOrientation()

    queue.async {
      self.stillImageOutput?.captureStillImageAsynchronously(from: connection) {
        buffer, error in

        guard let buffer = buffer, error == nil && CMSampleBufferIsValid(buffer),
          let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer),
          let image = UIImage(data: imageData)
          else {
            DispatchQueue.main.async {
              completion?()
            }
            return
        }

        self.savePhoto(image, location: location, completion: completion)
      }
    }
  }

  func savePhoto(_ image: UIImage, location: CLLocation?, completion: (() -> Void)? = nil) {
    PHPhotoLibrary.shared().performChanges({
      let request = PHAssetChangeRequest.creationRequestForAsset(from: image)
      request.creationDate = Date()
      request.location = location
    }, completionHandler: { _ in
        DispatchQueue.main.async {
          completion?()
        }
    })
  }

2 个答案:

答案 0 :(得分:1)

将其重写为:

}, completionHandler: { (_, _) in

根据documentationperformChanges(_:completionHandler:)中的完成处理程序接受两个参数,而不仅仅是一个参数。 _ in,您使用的是仅占用单个参数的占位符。

答案 1 :(得分:0)

正如您可以在错误消息中清楚地看到completionHandler传递两个参数而不只是一个。

所以你必须写

}, completionHandler: { (_, _) in

但强烈建议您处理结果和潜在错误。忽略错误会导致糟糕的用户体验。