gpt4 book ai didi

ios - 使用 AVFoundation 终止应用程序错误的自定义相机 - SWIFT 4

转载 作者:行者123 更新时间:2023-11-28 05:54:47 26 4
gpt4 key购买 nike

我正在尝试在 Swift 4、Xcode 9 和 AVFoundation 中制作一个自定义相机,我已经编写了所有代码来制作它,以便应用程序的 previewLayer 显示,你可以捕捉一个photo 但是当我运行它时没有任何反应并且我收到终止应用程序错误:

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

这是我编写的代码。我只有 1 个 ViewController 用于 1 个带有捕获按钮的 Storyboard和一个用于 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) {



}

}

最佳答案

检查您的 socket 连接。有一个 Controller 对象连接到“previewView”,但它不是在您的 Controller 中创建的。删除该连接并尝试。它会起作用。

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

关于ios - 使用 AVFoundation 终止应用程序错误的自定义相机 - SWIFT 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51783131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com