Skip to content
This repository has been archived by the owner on Jun 13, 2019. It is now read-only.

Photo capture with presser .photo So slow. #68

Open
Alexsmile2010 opened this issue Jan 4, 2017 · 0 comments
Open

Photo capture with presser .photo So slow. #68

Alexsmile2010 opened this issue Jan 4, 2017 · 0 comments

Comments

@Alexsmile2010
Copy link

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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant