gpt4 book ai didi

ios - Swift Playground iPad 相机访问

转载 作者:搜寻专家 更新时间:2023-10-31 19:36:34 25 4
gpt4 key购买 nike

在适用于 iPad 的 Swift Playground 应用程序的最新更新说明中,我看到可以通过 playgrounds 访问相机。因此,出于测试目的,我创建了一个 playground 来访问我的 iPad 后置摄像头。作为引用,我使用了这些来源:

这是我的代码:

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()

请忽略 session 的私有(private)变量等。我知道我可以/应该将它们设为本地方法。

我唯一看到的是 View 的棕色背景。 enter image description here第一次运行该应用程序时,iOS 询问我 Playgrounds 应用程序是否应该可以访问我接受的相机。在设置中,我还可以看到该应用程序可以访问相机。

iOS 版本:10.3.3 Playground 应用:1.6.1

最佳答案

它看起来只适用于 iOS 11。我在装有 iOS 10.3 的 iPad 上检查了你的代码,视频预览是空的。相同的代码在普通应用程序中运行得很好。在另一台运行 iOS 11 的平板电脑上,同样的代码运行得非常棒。

关于ios - Swift Playground iPad 相机访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46506623/

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