快速拍摄使用相机拍摄的图像

时间:2015-08-05 02:43:11

标签: ios swift camera

我有一个用户可以拍照的应用程序,但我只需要保存我用作面具的UIView区域(背景清晰),当我尝试进行裁剪时这样做错了,我怀疑图像大于屏幕和裁剪区域它的工作正常,但参考图像而不是屏幕区域。

这是我的代码:(cameraFrame是掩码视图)

if let videoConnection = output.connectionWithMediaType(AVMediaTypeVideo) {
            output.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {(sampleBuffer, error) in
                var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                var dataProvider = CGDataProviderCreateWithCFData(imageData)
                var cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, kCGRenderingIntentDefault)

                var cropped = CGImageCreateWithImageInRect(cgImageRef, self.cameraFrame.frame)
                self.imagen = UIImage(CGImage: cropped, scale: 1.0, orientation: UIImageOrientation.Right)
                self.performSegueWithIdentifier("aImagen", sender: self)
            })
        }

这是运行的应用: enter image description here

1 个答案:

答案 0 :(得分:1)

我正在使用以下pod

https://github.com/AlexLittlejohn/ALCameraViewController

使用

let croppingEnabled = true
let cameraViewController = ALCameraViewController(croppingEnabled: croppingEnabled) { image in
    // Do something with your image here. 
    // If cropping is enabled this image will be the cropped version
}

presentViewController(cameraViewController, animated: true, completion: nil)