iOS 开发技巧大全iOS开发记录iOS Blog

iOS 自定义相机捕捉

2015-12-15  本文已影响507人  ShawnDu

最基本的捕获图像

let session = AVCaptureSession()
let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
        for device in devices {
            if device.position == position {
                captureDevice = device as! AVCaptureDevice
            }
        }

position为init时指定的前置或者后置相机位置

do {
            input = try AVCaptureDeviceInput.init(device: captureDevice)
            if session.canAddInput(input) {
                session.addInput(input)
            }
        } catch {
            print("add video input error")
        }
}
previewLayer = AVCaptureVideoPreviewLayer.init(session: session)
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
 previewLayer.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kCameraBottomHeight - kNavigationHeight)

使用相机

camera = SSCamera.init(sessionPreset: AVCaptureSessionPresetPhoto, position: .Front)
cameraView.preview.layer.addSublayer(camera.previewLayer)

拍完照图像翻转

outputImage = UIImage(CGImage: inputImage.CGImage!, scale: inputImage.scale, orientation: UIImageOrientation.LeftMirrored)
UIGraphicsBeginImageContext(size)
inputImage.drawInRect(CGRectMake(0,0, size.width, size.height))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

github地址

微信公众号

开发者思维 devthinking

QQ交流群:295976280

iOS交流群(一)群二维码.png
上一篇下一篇

猜你喜欢

热点阅读