从 AVCapturePhotoCaptureDelegate 获取照片数据

Getting Photo Data from AVCapturePhotoCaptureDelegate

我目前正在尝试构建一个相机应用程序,只是为了好玩 运行 遇到了问题。

我使用的是 Apple 为您提供的拍摄照片或视频的示例。在这里找到:

https://developer.apple.com/library/content/samplecode/AVCam/Introduction/Intro.html

我在使用 PhotoCaptureDelegate 时遇到问题。所以目前当用户拍照时它会将它保存到用户的照片库中。我想做的是在拍摄照片后我想存储照片的 photoData 对象。这样我就可以在您保存之前在另一个视图中显示照片作为预览。

这是 Apple 建议使用的:

  func capture(_ captureOutput: AVCapturePhotoOutput, didFinishCaptureForResolvedSettings resolvedSettings: AVCaptureResolvedPhotoSettings, error: Error?) {
    if let error = error {
        print("Error capturing photo: \(error)")
        didFinish()
        return
    }

    guard let photoData = photoData else {
        print("No photo data resource")
        didFinish()
        return
    }

    PHPhotoLibrary.requestAuthorization { [unowned self] status in
        if status == .authorized {
            PHPhotoLibrary.shared().performChanges({ [unowned self] in
                    let creationRequest = PHAssetCreationRequest.forAsset()
                    creationRequest.addResource(with: .photo, data: photoData, options: nil)

                    if let livePhotoCompanionMovieURL = self.livePhotoCompanionMovieURL {
                        let livePhotoCompanionMovieFileResourceOptions = PHAssetResourceCreationOptions()
                        livePhotoCompanionMovieFileResourceOptions.shouldMoveFile = true
                        creationRequest.addResource(with: .pairedVideo, fileURL: livePhotoCompanionMovieURL, options: livePhotoCompanionMovieFileResourceOptions)
                    }

                }, completionHandler: { [unowned self] success, error in
                    if let error = error {
                        print("Error occurered while saving photo to photo library: \(error)")
                    }

                    self.didFinish()
                }
            )
        }
        else {
            self.didFinish()
        }
    }
}

我如何从我的委托中获取 photoData 对象并将其传递回调用该委托的视图控制器?

有几种方法可以解决这个问题。您还可以让您的视图控制器拥有照片捕获委托(如 Apple 的示例)并在拍摄照片后将其传回。你也可以让你的视图控制器成为照片捕获委托而不用担心传递任何东西!

1) 上传照片

(再次)有几种方法可以做到这一点。您可以让委托将值传回控制器。您还可以让代表告诉控制器它已完成,然后让控制器过来抓取照片。我将在下面解释第二种方式。

看看苹果的例子,我们可以看到 PhotoCaptureDelegate 已经在完成时告诉控制器!每当创建 PhotoCaptureDelegate 对象时,它都会传递一个 completed 块。

为了让控制器抓取照片,我们只需要制作照片对象public!

  1. PhotoCaptureDelegate中的private var photoData: Data? = nil改为public var photoData: Data? = nil
  2. CameraViewController定义completed块时,您现在有照片数据:
    ...
    , completed: { [unowned self] photoCaptureDelegate in
        self.sessionQueue.async { [unowned self] in
            self.inProgressPhotoCaptureDelegates[photoCaptureDelegate.requestedPhotoSettings.uniqueID] = nil
            if let photoData = photoCaptureDelegate.photoData {
                // You now have the photo data!
                // You can pass this to another method in the Controller now                
            }
        }
    }
    ...

2) View Controller 作为 AVCapturePhotoCaptureDelegate

解决此问题的另一种方法是让您的 ViewController 成为 AVCapturePhotoCaptureDelegate。然后你已经在视图控制器中有了照片数据!它可能看起来像这样:

class MyViewController: UIViewController, AVCapturePhotoCaptureDelegate {

    ...

    func capture(_ captureOutput: AVCapturePhotoOutput, didFinishCaptureForResolvedSettings resolvedSettings: AVCaptureResolvedPhotoSettings, error: Error?) {
        guard let photoData = photoData else {
            return
        }
        // You now have your photo data!
    }
}