gpt4 book ai didi

swift - 使用 iPhone 相机的 macOS App

转载 作者:行者123 更新时间:2023-12-04 17:47:12 24 4
gpt4 key购买 nike

我正在尝试构建一个简单的 swift (4) macOS 应用程序来使用连接到我的 Mac 的 iPhone 相机。
我已经启动了一个空白的 macOS 模板应用程序并打开了沙箱以允许相机、麦克风和 USB 并将以下代码添加到我的 ViewController。

import Cocoa
import AVFoundation

class ViewController: NSViewController {
@IBOutlet weak var camera: NSView!

override func viewDidLoad() {
super.viewDidLoad()
camera.layer = CALayer()
let session:AVCaptureSession = AVCaptureSession()
session.sessionPreset = AVCaptureSession.Preset.high
let device:AVCaptureDevice = (AVCaptureDevice.default(for: AVMediaType.video))!
// let listdevices = (AVCaptureDevice.devices())


do {
try session.addInput(AVCaptureDeviceInput(device: device))

//Preview
let previewLayer:AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: session)
let myView:NSView = self.view
previewLayer.frame = myView.bounds
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
self.camera.layer?.addSublayer(previewLayer)
session.startRunning()

// print(listdevices)
// print(device)
} catch {
print(device)
}


}


override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}


}

在 Storyboard中,我放入了自定义 View 。

应用程序构建正常并且使用 facetime 摄像头没问题,但是连接 iPhone 时我不知道作为 AVFoundation 可以使用的设备。不确定如何获取 previewLayer 以选择 USB 摄像头以使用 aka iPhone。

p.s 所有相机方向都需要风景

最佳答案

根据this Apple Developer Forum 帖子,不支持从 macOS 应用程序捕获连接的 iOS 设备的相机。
您可以做的最接近的事情(如帖子所示)是捕获 相机 (Camera.app) 运行时 iOS 设备的 View ,有效地捕获实时相机预览(或者,如果您想从捕获的屏幕中删除相机应用程序的 UI,您可以在 iOS 中滚动您自己的配套相机应用程序)。

关于swift - 使用 iPhone 相机的 macOS App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47927191/

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