我在Swift3中构建了一个相机应用程序。当我尝试在iPhone上运行我的应用程序时,它会显示以下错误。不知道为什么或做什么。
import UIKit
import AVFoundation
class ViewController: UIViewController {
@IBOutlet weak var previewView:UIView!
var session = AVCaptureSession()
var photoOutput = AVCapturePhotoOutput()
let notification = NotificationCenter.default
override func viewDidLoad() {
super.viewDidLoad()
if session.isRunning {
return
}
setupInputOutput()
setPreviewLayer()
session.startRunning()
notification.addObserver(self,
selector: #selector(self.changedDeviceOrientation(_:)),
name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
@IBAction func takePhoto(_ sender: AnyObject) {
let captureSetting = AVCapturePhotoSettings()
captureSetting.flashMode = .auto
captureSetting.isAutoStillImageStabilizationEnabled = true
captureSetting.isHighResolutionPhotoEnabled = false
photoOutput.capturePhoto(with: captureSetting, delegate: self)
}
func setupInputOutput(){
session.sessionPreset = AVCaptureSessionPresetPhoto
do {
let device = AVCaptureDevice.defaultDevice(
withDeviceType: AVCaptureDeviceType.builtInWideAngleCamera,
mediaType: AVMediaTypeVideo,
position: .back)
let input = try AVCaptureDeviceInput(device: device)
if session.canAddInput(input){
session.addInput(input)
}else {
print("セッションに入力を追加できなかった")
return
}
} catch let error as NSError {
print("カメラがない\(error)")
return
}
if session.canAddOutput(photoOutput) {
session.addOutput(photoOutput)
} else {
print("セッションに出力をできなかった")
return
}
}
func setPreviewLayer(){
let previewLayer = AVCaptureVideoPreviewLayer(session: session)
guard let videoLayer = previewLayer else {
print("プレビューレイヤーを作れなかった")
return
}
videoLayer.frame = view.bounds
videoLayer.masksToBounds = true
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
//previewViewに追加する
previewView.layer.addSublayer(videoLayer)
}
func changedDeviceOrientation(_ notification :Notification) {
if let photoOutputConnection = self.photoOutput.connection(withMediaType: AVMediaTypeVideo) {
switch UIDevice.current.orientation {
case .portrait:
photoOutputConnection.videoOrientation = .portrait
case .portraitUpsideDown:
photoOutputConnection.videoOrientation = .portraitUpsideDown
case .landscapeLeft:
photoOutputConnection.videoOrientation = .landscapeRight
case .landscapeRight:
photoOutputConnection.videoOrientation = .landscapeLeft
default:
break
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
它无法运行。当我尝试运行时," ETC_BREAKPOINT(代码= 1,子代码= 0x1002b51fc"出现。没有代码错误。
答案 0 :(得分:0)
向您添加uncaughtExceptionHandler应用委托并打印出堆栈 有关详情,请参阅此帖子:Register UncaughtExceptionHandler in Objective C using NSSetUncaughtExceptionHandler
如果你知道异常的位置
,你也可以添加一个try-catch-finally块