Проблема с плохим выполнением AVCaptureSession Swift 3?

Я пытаюсь настроить пользовательскую камеру в Swift 3 для iOS 10.1.

Я продолжаю получать сообщение об ошибке ниже

«[MC] Контейнер системной группы для пути systemgroup.com.apple.configurationprofiles — /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles Чтение общедоступных эффективных пользовательских настроек».

Я попытался добавить «Конфиденциальность - Описание использования камеры» в info.plist и микрофон, но проблема все еще есть.

Иногда, когда я отключаю свой iPhone от кода, появляется сообщение для авторизации камеры, как будто она «застряла» и «перестала» появляться?

Кто-нибудь знает, как обойти использование 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.
}


}

person Matt    schedule 30.10.2016    source источник


Ответы (1)


Решил проблему!

Правильный код ниже:

    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")
            }
        }
    }
}
person Matt    schedule 06.11.2016
comment
Это запускает камеру, но как вы представляете это на экране? Извините, я новичок с камерой на IOS 10. - person MLBDG; 16.02.2017
comment
Я имею в виду, как только вы свяжете UIView в раскадровке с @IBOullet var cameraView: UIView! камера не показывает (да, права камеры выставлены в info.plist) - person MLBDG; 16.02.2017