-1

I'm building an camera app in Swift3. When I try running my app on my iPhone it displays the following error. Not sure why or what to do.

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


}

  it can't be running. when I try it running, "ETC_BREAKPOINT(code=1,subcode=0x1002b51fc" is appeared.there's no code error.

tetsu
  • 13
  • 2
  • Can you provide more info? Maybe a code snippet? – Bill Abt Jan 27 '17 at 02:44
  • Does it run at all? Did you try the simulator first? In the XCode debugger setting breakpoints in your AppDelegate methods and see what's being called at the start of the execution. If you're getting that far, set more breakpoints around your code, and continue the execution until you narrow down where it's failing. Search for the very *first* methods that gets called for any apps when they start and put breakpoints there. Try to intercept it *before* the bug is hit, while running in the simulator. Simulator is faster. if it only happens on the phone, do same with debugger with phone. – clearlight Jan 27 '17 at 02:47
  • Where's your code? Please, how can we help you without something coded in Swift? The supposed code you added isn't code at all. Flagged. –  Jan 27 '17 at 04:34
  • Have you added the Plist entry for camera access ? – Sean Lintern Jan 27 '17 at 11:01

1 Answers1

0

Add an uncaughtExceptionHandler to you App delegate and print out the stack See this post for more info: Register UncaughtExceptionHandler in Objective C using NSSetUncaughtExceptionHandler

You can also add a try-catch-finally block if you know where the exception is

Community
  • 1
  • 1
Shravya Boggarapu
  • 572
  • 1
  • 7
  • 23