添加相机背景(swift)

前端之家收集整理的这篇文章主要介绍了添加相机背景(swift)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

添加相机背景(swift)

  • 1.定义CaptureSession
var captureSession : AVCaptureSession?
var stillImageOutput : AVCaptureStillImageOutput?
var previewLayer : AVCaptureVideoPreviewLayer?
  • 2.初始化
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    captureSession = AVCaptureSession()
    captureSession?.sessionPreset = AVCaptureSessionPreset1920x1080
    let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    var error : NSError?
    var input: AVCaptureDeviceInput!

    do {
        input = try AVCaptureDeviceInput(device: backCamera)
    } catch let error1 as NSError {
        error = error1
        input = nil
    }

    if (error == nil && captureSession?.canAddInput(input) != nil){
        captureSession?.addInput(input)
        stillImageOutput = AVCaptureStillImageOutput()
        stillImageOutput?.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]
        if (captureSession?.canAddOutput(stillImageOutput) != nil){
            captureSession?.addOutput(stillImageOutput)
            previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
            previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
            previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.Portrait
            self.view.layer.addSublayer(previewLayer!)
            captureSession?.startRunning()
        }
    }
}
原文链接:https://www.f2er.com/swift/324651.html

猜你在找的Swift相关文章