Matt Matt - 29 days ago 28
Swift Question

AVCaptureSession Swift 3 bad exec issue?

I am trying to set up a custom camera within swift 3 for iOS 10.1.

I keep getting the error message below


" [MC] System group container for
systemgroup.com.apple.configurationprofiles path is
/private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
Reading from public effective user settings. "


I have tried adding "Privacy - Camera Usage Description" into info.plist, and the microphone one, but still have the issue.

Sometimes when I disconnect my iPhone from code the message to authorise the camera comes up, almost as if it was "stuck" and "stopped" from coming up?

Does anyone know how to get around using AVCaptureStillImageOutput? It has been deprecated in iOS 10 onwards, and I want to make my app a little bulletproof for the future.

import UIKit
import AVFoundation

class ViewController: UIViewController {

var captureSession : AVCaptureSession?
var stillImageOutput: AVCaptureStillImageOutput?
var previewLayer : AVCaptureVideoPreviewLayer?

@IBOutlet weak var cameraView: UIView!

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
captureSession?.sessionPreset = AVCaptureSessionPresetPhoto

let deviceDiscoverySession = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDuoCamera, AVCaptureDeviceType.builtInTelephotoCamera,AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.unspecified)
for device in (deviceDiscoverySession?.devices)! {
if device.position == AVCaptureDevicePosition.front{
do {
let input = try AVCaptureDeviceInput(device: device)
if (captureSession?.canAddInput(input))!{
captureSession?.addInput(input)
stillImageOutput?.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
}
if (captureSession?.canAddOutput(stillImageOutput))! {
captureSession?.addOutput(stillImageOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
cameraView.layer.addSublayer(previewLayer!)
captureSession?.startRunning()
}
} catch{
print("Error Occured when trying get camera")
}
}
}
}


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

Answer

Solved the issue!

Correct code below:

    override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // setting up the camera session
    captureSession = AVCaptureSession()
    captureSession?.sessionPreset = AVCaptureSessionPreset1920x1080
    let deviceDiscoverySession = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDuoCamera, AVCaptureDeviceType.builtInTelephotoCamera,AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.front)
    for device in (deviceDiscoverySession?.devices)! {
        if device.position == AVCaptureDevicePosition.front{
            do {
                let input = try AVCaptureDeviceInput(device: device)
                if (captureSession?.canAddInput(input))!{
                    captureSession?.addInput(input)
                    stillImageOutput = AVCaptureStillImageOutput()
                    stillImageOutput?.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
                if (captureSession?.canAddOutput(stillImageOutput))! {
                    captureSession?.addOutput(stillImageOutput)
                    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                    previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
                    previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
                    cameraView.layer.addSublayer(previewLayer!)
                    captureSession?.startRunning()
                    }
                }
            } catch{
                print("Error Occured when trying get camera")
            }
        }
    }
}
Comments