user10219864
user10219864

Reputation:

Camera View as blurred background?

I try to use my camera's view (blurred) as background in my main menu.

I'm a beginner and have no idea how to do this...

please don't answer with "use ARSCNView"; I've tried this.

(best: send me a code in swift; I shouldn't be too long huh?)

After trying your code I have these errors: my code

Upvotes: 1

Views: 2041

Answers (1)

Mahesh Dangar
Mahesh Dangar

Reputation: 804

import UIKit
import AVFoundation

class TestingViewController: UIViewController {

    let session: AVCaptureSession = AVCaptureSession()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        session.sessionPreset = AVCaptureSession.Preset.high

        if let device = AVCaptureDevice.default(for: AVMediaType.video) {
            do {
                try session.addInput(AVCaptureDeviceInput(device: device))

            } catch {
                print(error.localizedDescription)
            }

            let previewLayer = AVCaptureVideoPreviewLayer(session: session)

            self.view.layer.addSublayer(previewLayer)
            previewLayer.frame = self.view.layer.bounds

        }


        session.startRunning()

        let blur = UIBlurEffect(style: .regular)
        let blurView = UIVisualEffectView(effect: blur)
        blurView.frame = self.view.bounds
        blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.view.addSubview(blurView)


    }

}

try this updated code and dont forget to add

"Privacy - Camera Usage Description" in your info.plist file

Upvotes: 2

Related Questions