【问题标题】:Custom Camera using AVFoundation terminating app error - SWIFT 4使用 AVFoundation 终止应用程序错误的自定义相机 - SWIFT 4
【发布时间】:2019-01-17 20:52:56
【问题描述】:

我正在尝试在 Swift 4、Xcode 9 中使用 AVFoundation 制作自定义相机,并且我已经编写了所有代码来制作它,以便应用程序的 previewLayer 显示并且您可以拍摄照片但是当我运行时它什么也没发生,我收到一个终止应用程序错误:

2018-08-10 09:06:31.160691+0200 Yubi[313:12221] *** 终止应用 由于未捕获的异常 'NSUnknownKeyException',原因: '[setValue:forUndefinedKey:]: 此类与键 previewView 的键值编码不兼容。'

这是我编写的代码。我只有 1 个 ViewController 用于 1 个带有捕获按钮的故事板和用于 previewLayer 的 UIView:

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) {



    }

}

【问题讨论】:

  • 检查您的插座连接。有一个控制器对象与未在您的控制器中创建的“previewView”连接。删除该连接并尝试。它会起作用的。
  • 解决了吗?

标签: ios swift xcode error-handling camera


【解决方案1】:

您似乎在某处使用previewView。在故事板或 XIB 中检查您的 ViewController 的 IBConnections。您可能已将previewView 连接到某个元素。如果您删除它,您的问题将得到解决。

【讨论】:

    【解决方案2】:

    检查您的插座连接。有一个控制器对象与未在您的控制器中创建的“previewView”连接。删除该连接并尝试。它会起作用的。

    您将看到带有移动类型图标的 previewView,这意味着它不是 在控制器中声明。检查附加的图像以将其删除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多