Skip to content

Instantly share code, notes, and snippets.

@ulope
Last active July 10, 2021 05:21
Show Gist options
  • Save ulope/5f7973968f78188e52f388cbe6446f1e to your computer and use it in GitHub Desktop.
Save ulope/5f7973968f78188e52f388cbe6446f1e to your computer and use it in GitHub Desktop.
iOS fullscreen camera preview without UI

iOS Fullscreen UI-less camera preview

I recently had a need for an app that displayed a fullscreen camera preview on an iPhone without any UI whatsoever. This is the result of 30 minutes of cobbling together various SO answers, blog posts etc.

All you need to do to use this is:

  • Create a new "Single view" poject
  • Add a new UIView to the ViewController in the Storyboard
  • Connect the outlet previewView to the UIView
  • Add the entries below to Info.plist
    <key>NSMicrophoneUsageDescription</key>
    <string>This app does not require access to the microphone.</string>
    <key>NSCameraUsageDescription</key>
    <string>This app requires access to the camera.</string>
//
// ViewController.swift
// nouicam
//
// Created by Ulrich Petri on 27.04.2017.
// Copyright © 2017 Ulrich Petri.
//
// License: MIT
//
import UIKit
import AVFoundation
extension AVCaptureVideoOrientation {
var uiInterfaceOrientation: UIInterfaceOrientation {
get {
switch self {
case .landscapeLeft: return .landscapeLeft
case .landscapeRight: return .landscapeRight
case .portrait: return .portrait
case .portraitUpsideDown: return .portraitUpsideDown
}
}
}
init(ui:UIInterfaceOrientation) {
switch ui {
case .landscapeRight: self = .landscapeRight
case .landscapeLeft: self = .landscapeLeft
case .portrait: self = .portrait
case .portraitUpsideDown: self = .portraitUpsideDown
default: self = .portrait
}
}
init?(orientation:UIDeviceOrientation) {
switch orientation {
case .landscapeRight: self = .landscapeLeft
case .landscapeLeft: self = .landscapeRight
case .portrait: self = .portrait
case .portraitUpsideDown: self = .portraitUpsideDown
default:
return nil
}
}
}
class ViewController: UIViewController {
@IBOutlet weak var previewView: UIView!
var session: AVCaptureSession?
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
session = AVCaptureSession()
session!.sessionPreset = AVCaptureSessionPreset1920x1080
let backCamera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
var error: NSError?
var input: AVCaptureDeviceInput!
do {
input = try AVCaptureDeviceInput(device: backCamera)
} catch let error1 as NSError {
error = error1
input = nil
print(error!.localizedDescription)
}
if error == nil && session!.canAddInput(input) {
session!.addInput(input)
}
let videoOutput = AVCaptureVideoDataOutput()
if session!.canAddOutput(videoOutput) {
session!.addOutput(videoOutput)
}
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: session)
videoPreviewLayer!.videoGravity = AVLayerVideoGravityResizeAspect
videoPreviewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
previewView.layer.addSublayer(videoPreviewLayer!)
session!.startRunning()
}
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
videoPreviewLayer!.frame = previewView.bounds
}
override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
videoPreviewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation(ui: toInterfaceOrientation)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
videoPreviewLayer!.frame = previewView.bounds
}
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.isIdleTimerDisabled = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override var prefersStatusBarHidden: Bool {
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment