1

我目前正在尝试构建一个有趣的相机应用程序并遇到了问题。

我正在使用 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从我的委托中获取对象并将其传递回我的视图控制器,即调用此委托?

4

1 回答 1

2

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

1) 把照片传回去

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

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

为了让控制器抓取照片,我们只需要将照片对象公开!

  1. private var photoData: Data? = nil将in更改PhotoCaptureDelegatepublic 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!
    }
}
于 2017-01-29T06:08:08.470 回答