Photo capture with presser .photo So slow.
Opened this issue · 0 comments
Alexsmile2010 commented
After photo capture I need to crop image by layer rect. Presset option is .photo. `func capturePhoto(_ blockCompletion: @escaping blockCompletionCapturePhoto) {
let connectionVideo = self.stillCameraOutput.connection(withMediaType: AVMediaTypeVideo)
connectionVideo?.videoOrientation = AVCaptureVideoOrientation.orientationFromUIDeviceOrientation(UIDevice.current.orientation)
self.stillCameraOutput.captureStillImageAsynchronously(from: connectionVideo) { (sampleBuffer: CMSampleBuffer?, err: Error?) -> Void in
if let err = err {
blockCompletion(nil, err as NSError?)
}
else
{
if let sampleBuffer = sampleBuffer, let dataImage = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
{
var image = CIImage(data: dataImage)
let outputRect = self.previewLayer?.metadataOutputRectOfInterest(for: (self.previewLayer?.bounds)!)
var takenCGImage = CIContext(options: nil).createCGImage(image!, from: (image?.extent)!)
let width = CGFloat((takenCGImage?.width)!)
let height = CGFloat((takenCGImage?.height)!)
let cropRect = CGRect(x: outputRect!.origin.x * width, y: outputRect!.origin.y * height, w: outputRect!.size.width * width, h: outputRect!.size.height * height)
var cropCGImage = takenCGImage!.cropping(to: cropRect)
let imgOrientation: UIImageOrientation?
if self.cameraDevicePosition == .back
{
imgOrientation = .right
}
else
{
imgOrientation = .leftMirrored
}
let finalImage = UIImage(cgImage: cropCGImage!, scale: 1, orientation: imgOrientation!)
blockCompletion(finalImage, nil)
}
else
{
blockCompletion(nil, nil)
}
}
}`
This operation is very slow. Can I do it faster?