From c0a95613f184624af3de2afd0bde16facb7e1473 Mon Sep 17 00:00:00 2001 From: jiaqiang Date: Mon, 28 Mar 2016 23:14:05 +0800 Subject: [PATCH] Add a capture picture callback to pass on image data Sometimes, user want to store the picture by the other ways, such as a new album, sand disk and so on. Or cutting, compounding, adding filter. --- camera/CameraManager.swift | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/camera/CameraManager.swift b/camera/CameraManager.swift index c9615e8..f2c5650 100644 --- a/camera/CameraManager.swift +++ b/camera/CameraManager.swift @@ -357,6 +357,37 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGe _show(NSLocalizedString("No capture session setup", comment:""), message: NSLocalizedString("I can't take any picture", comment:"")) } } + + /** + Captures still image from currently running capture session. + + :param: imageCompletition Completition block containing the captured imageData + */ + public func capturePictureDataWithCompletition(imageCompletition: (NSData?, NSError?) -> Void) { + if cameraIsSetup { + if cameraOutputMode == .StillImage { + dispatch_async(sessionQueue, { + self._getStillImageOutput().captureStillImageAsynchronouslyFromConnection(self._getStillImageOutput().connectionWithMediaType(AVMediaTypeVideo), completionHandler: { [weak self] (sample: CMSampleBuffer!, error: NSError!) -> Void in + if (error != nil) { + dispatch_async(dispatch_get_main_queue(), { + if let weakSelf = self { + weakSelf._show(NSLocalizedString("Error", comment:""), message: error.localizedDescription) + } + }) + imageCompletition(nil, error) + } else { + let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sample) + imageCompletition(imageData, nil) + } + }) + }) + } else { + _show(NSLocalizedString("Capture session output mode video", comment:""), message: NSLocalizedString("I can't take any picture", comment:"")) + } + } else { + _show(NSLocalizedString("No capture session setup", comment:""), message: NSLocalizedString("I can't take any picture", comment:"")) + } + } /** Starts recording a video with or without voice as in the session preset.