Custom Camera using AVFoundation terminating app error - SWIFT 4

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Custom Camera using AVFoundation terminating app error - SWIFT 4



I am trying to make a custom camera in Swift 4, Xcode 9 with AVFoundation and I've written all the code to make it so that the previewLayer of the app displays and you can capture a photo but when I run it nothing happens and I get a Terminating app error:


previewLayer



2018-08-10 09:06:31.160691+0200 Yubi[313:12221] *** Terminating app
due to uncaught exception 'NSUnknownKeyException', reason:
'[ setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key previewView.'



Here is the code I've written. I only have 1 ViewController for 1 storyboard with a capture button and a UIView for the previewLayer:


ViewController


previewLayer


import UIKit
import Foundation
import AVFoundation

class CameraViewController: UIViewController {

//This is the IBoutlet for the camera view (so the live feed of what u see)
@IBOutlet weak var cameraView: UIView!
@IBOutlet weak var button: UIButton!

var captureSession = AVCaptureSession()
var sessionOutput = AVCapturePhotoOutput()
var previewLayer = AVCaptureVideoPreviewLayer()

override func viewDidLoad()
super.viewDidLoad()

// Do any additional setup after loading the view.


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

//To get a devie session and choose what side camera i think. In the Devicetypes: i am declaring what type of camera im looking for in the telephones devices (so functional pieces of hardware)
let deviceSession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera,.builtInDualCamera,.builtInTelephotoCamera], mediaType: AVMediaType.video, position: .unspecified)

for device in (deviceSession.devices)

if device.position == AVCaptureDevice.Position.front

do

let input = try AVCaptureDeviceInput(device: device)

if captureSession.canAddInput(input)
captureSession.addInput(input)

if captureSession.canAddOutput(sessionOutput)
captureSession.addOutput(sessionOutput)

previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
previewLayer.connection?.videoOrientation = .portrait


cameraView.layer.addSublayer(previewLayer)
cameraView.addSubview(button)


previewLayer.position = CGPoint(x: self.cameraView.frame.width / 2, y: self.cameraView.frame.height / 2)

previewLayer.bounds = cameraView.frame

captureSession.startRunning()





catch let avError
print(avError)









//if let cameraID = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.front).devices.first?.localizedName
//cameraID = "Front Camera"


//This is for the actuall take photo button
@IBAction func takePhoto(_ sender: Any)










Check your outlet connection. There is one controller object which connected with "previewView" where it's not created in your controller. Remove that connection and try. It will work.
– Ayaz Rafai
Aug 10 at 9:31





Is that solved?
– Ayaz Rafai
Aug 10 at 10:29




2 Answers
2



Check your outlet connection. There is one controller object which connected with "previewView" where it's not created in your controller. Remove that connection and try. It will work.



You will see previewView with mobile type icon which mean that is not
declared in controller. Check attached image to remove that.



enter image description here



It seems as you are using previewView somewhere. Check your ViewController's IBConnections in storyboard or XIB. There are possibilities that you may have connected previewView to some element. If you remove that your problem will get solved.


previewView


previewView






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS