remirobert / CameraEngine

:monkey::camera: Camera engine for iOS, written in Swift, above AVFoundation. :monkey:
https://github.com/remirobert/CameraEngine
MIT License
583 stars 104 forks source link

Photo capture with presser .photo So slow. #68

Open Alexsmile2010 opened 7 years ago

Alexsmile2010 commented 7 years ago

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?