ios – Swift Playground iPad摄像头访问

在针对iPad的
Swift Playground应用程序的最新更新说明中,我看到可以通过操场访问摄像机.

出于测试目的,我因此创建了一个操场来访问我的iPad后置摄像头.

作为参考,我使用了这些来源:

> https://github.com/codepath/ios_guides/wiki/Creating-a-Custom-Camera-View
> https://github.com/francip/swift-playground-camera/blob/master/Camera.playground/Contents.swift

这是我的代码:

import UIKit
import AVFoundation
import PlaygroundSupport

class MainViewController : UIViewController {
    private var _session: AVCaptureSession?
    private var _captureInput: AVCaptureInput?
    private var _stillImageOutput: AVCaptureStillImageOutput?
    private var _frontCamera: AVCaptureDevice?
    private var _previewView: UIView?
    private var _previewLayer: AVCaptureVideoPreviewLayer?

public override func loadView() {
    print("loadView()")

    _previewView = UIView()
    guard let view = _previewView else {
        printError(errMsg: "View could not be created!")
        return
    }
    view.backgroundColor = UIColor.brown
    self.view = view
}

public override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // Create a capture session
    _session = AVCaptureSession()
    guard let session = _session  else {
        printError(errMsg: "Session is empty!")
        return
    }
    session.beginConfiguration()
    session.sessionPreset = AVCaptureSession.Preset.photo

    // Get access to front camera
    _frontCamera = AVCaptureDevice.default(for: AVMediaType.video)

    guard let frontCamera = _frontCamera
        else {
            printError(errMsg: "Front camera not accessible!")
            return
    }

    do{
        _captureInput = try AVCaptureDeviceInput(device: frontCamera)
    } catch let err as NSError {
        printError(errMsg: err.localizedDescription)
        return
    }

    // Add input to session
    guard let captureInput = _captureInput else {
        printError(errMsg: "Capture input not available!")
        return
    }

    if session.canAddInput(captureInput){
        session.addInput(captureInput)
    }

    // Configurw the image output
    _stillImageOutput = AVCaptureStillImageOutput()
    guard let sillImageOutput = _stillImageOutput else {
        printError(errMsg: "Image output not available!")
        return
    }

    sillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]

    if session.canAddOutput(sillImageOutput) {
        session.addOutput(sillImageOutput)
    }
    session.commitConfiguration()
    // Add video preview layer
    _previewLayer = AVCaptureVideoPreviewLayer(session: session)

    guard let previewLayer = _previewLayer else {
        printError(errMsg: "Preview layer not available!")
        return
    }

    previewLayer.videoGravity = AVLayerVideoGravity.resizeAspect
    previewLayer.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
    guard let view = _previewView else {
        printError(errMsg: "View not available!")
        return
    }
    view.layer.addSublayer(previewLayer)
    view.layer.borderWidth = 5

    // Start the capture session
    session.startRunning()
}

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

public override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    guard let previewLayer = _previewLayer else {
        printError(errMsg: "Preview layer not available!")
        return
    }
    guard let previewView = _previewView else {
        printError(errMsg: "View not available!")
        return
    }
    print(previewView.bounds)
    previewLayer.frame = previewView.bounds
}

private func printError(errMsg: String) {
    print("[Error]: " + errMsg)
}
}

PlaygroundPage.current.liveView = MainViewController()

请忽略会话的私有变量等.我知道我可以/应该使它们成为本地方法.

我唯一看到的是视图的棕色背景.
《ios – Swift Playground iPad摄像头访问》
当iOS第一次运行应用程序时,我问我Playgrounds应用程序是否应该访问我接受的相机.在设置中,我还可以看到应用程序可以访问相机.

iOS版本:10.3.3
游乐场应用程序:1.6.1

最佳答案 看起来它只能在iOS 11上运行.我在iPad上用iOS 10.3检查了你的代码,视频预览是空的.在普通应用程序中运行相同的代码.在另一台运行iOS 11的平板电脑上,相同的代码就像魅力一样.

点赞