AVCaptureSession Swift 3 错误的执行问题?

AVCaptureSession Swift 3 bad exec issue?

我正尝试在 swift 3 内为 iOS 10.1 设置自定义相机。

我不断收到以下错误消息

" [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. "

我试过将 "Privacy - Camera Usage Description" 添加到 info.plist 和麦克风,但问题仍然存在。

有时,当我断开 iPhone 与代码的连接时,会出现授权相机的消息,几乎就像 "stuck" 和 "stopped" 一样?

有人知道如何绕过 AVCaptureStillImageOutput 吗?从 iOS 10 开始,它已被弃用,我想让我的应用程序在未来变得更安全。

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.
}


}

问题解决了!

下面的正确代码:

    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")
            }
        }
    }
}