无法获取相机图像

时间:2017-01-05 10:29:42

标签: ios iphone swift camera

我创建了一个简单的应用程序,假设从iPhone摄像头向视图显示实时图像,将用Swift2编写的本教程修改为Swift3。

我使用Xcode 8.1并在iOS 6版本为9.3.4的iPhone 6上运行应用程序 我的Xcode部署目标设置是9.1

当我运行应用程序时,代码完成没有错误,但没有显示相机图像。该应用甚至无法显示使用相机的权限提示。

我做错了什么?

以下是我的完整代码。

import UIKit
import AVFoundation

class ViewController: UIViewController {

    // MARK: UI

    let cameraView: UIView = {
        let v = UIView()
        v.translatesAutoresizingMaskIntoConstraints = false
        return v
    }()

    // MARK: System

    var captureSession = AVCaptureSession()
    var sessionOutput  = AVCaptureStillImageOutput()
    var previewLayer   = AVCaptureVideoPreviewLayer()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupSubviews()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        cameraSetup()
    }

    func setupSubviews() {
        view.addSubview( cameraView )

        cameraView.backgroundColor = UIColor.orange
        cameraView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        cameraView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        cameraView.widthAnchor.constraint(equalToConstant: 200).isActive = true
        cameraView.heightAnchor.constraint(equalToConstant: 200).isActive = true

    }

    func cameraSetup() {

        print("**** Start Camera Setup ****")

        let devices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo)! as! [AVCaptureDevice]
        for device in devices {
            if device.position == .front {
                print("Front")

                do {
                    let input = try AVCaptureDeviceInput(device: device)

                    if captureSession.canAddInput(input) {
                        captureSession.addInput(input)
                        sessionOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]

                        print("Input added")

                        if captureSession.canAddOutput(sessionOutput) {
                            captureSession.addOutput(sessionOutput)
                            captureSession.startRunning()

                            previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                            previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
                            previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.portrait
                            cameraView.layer.addSublayer(previewLayer)
                            previewLayer.position = CGPoint(x: self.cameraView.frame.width / 2, y: self.cameraView.frame.height / 2)
                            previewLayer.bounds = cameraView.frame

                            print("Output added")
                        }
                    }


                } catch {
                    print("Error")
                }

            }
        }

        print("**** Finish Camera Setup ****")
    }

}

0 个答案:

没有答案