-
为 iPhone Duo 构建出色的相机体验
了解如何充分利用 iPhone Duo 的内外摄像头。探索 Virtual Front Camera,了解如何使用 Direction Coordinator 在摄像头之间切换,并在用户开合 iPhone Duo 时更新 UI。你还能学习处理预览宽高比、定位和旋转的最佳实践,以提供无缝的拍摄体验。
章节
- 0:00 - Introduction
- 0:31 - Meet the new front cameras
- 0:58 - Discover the virtual front camera
- 1:53 - Access each camera individually
- 2:46 - Track camera direction
- 4:03 - Create a direction coordinator
- 4:50 - Use both displays at once
- 5:38 - Work with device descriptors
- 6:17 - Handle a direction change
- 7:03 - Polish your camera preview
- 8:03 - Handle rotation
- 8:47 - Next steps
资源
相关视频
Tech Talks
WWDC26
-
搜索此视频…
大家好,我叫 塔伦,来自相机软件团队。 在本视频中, 我将介绍 iPhone Duo 上的新相机功能, 并讲解如何利用这些功能打造出色的用户体验。 首先,我将介绍 iPhone Duo 上的新前置摄像头。 然后,我将分享当用户打开或关闭设备时, 如何处理相机方向的变化。 最后,我将分享一些技巧,帮助你优化应用 在 iPhone Duo 上的相机预览效果。
现在,让我们来认识一下 iPhone Duo 的新前置摄像头。 iPhone Duo 是首款支持双前置 摄像头的 iPhone。
这两颗摄像头均采用方形传感器, 具备超广角视野。 在设备外侧, 你会看到外侧的超广角摄像头。 它能拍摄出绝佳的照片和视频。 打开设备,你会发现内侧的超广角摄像头—— 这是 iPhone 上首款屏下摄像头。
你的应用可以通过 AVFoundation 从前置摄像头进行流媒体传输。 与其他 iPhone 一样, 使用发现会话 AVCaptureDeviceDiscoverySession 来查找 前置摄像头。
请求 position.front, 并指定 Wide 或 Ultra Wide 设备类型。 当您在 iPhone Duo 上执行此操作时,您的应用 将发现新的“虚拟前置摄像头”。
“虚拟前置摄像头”是一种 新的 AVCaptureDevice, 它会在外屏和内屏的物理摄像头之间自动切换。 当您打开 iPhone Duo 时, “虚拟前置摄像头”会使用内屏上的前置摄像头。
当您合上 iPhone 时,摄像头会自动 切换到外屏上的前置摄像头。 虚拟前置摄像头会为您的应用 选择最合适的前置摄像头。 虚拟前置摄像头会自动 为您处理摄像头切换。 但您还可以通过 响应摄像头方向的变化来实现更多功能。
在 iPhone Duo 上,每台前置摄像头都有其 对应的 AVCaptureDevice 类型。
使用内置的外屏超广角设备类型, 即可访问外屏上的前置摄像头。 使用内置的内屏超广角设备类型, 即可访问内屏上的前置摄像头。 使用这些设备类型时, 您可以访问每台摄像头的全部功能。 内屏超广角摄像头支持 1080p 视频分辨率, 最高帧率为 60fps。 外屏超广角摄像头支持最高 4K 分辨率和 120fps。 当您的应用使用虚拟前置摄像头时, 您只能访问两台物理摄像头共有的功能。 即最高 1080p 分辨率 和 60fps。 另外需要注意的是,只有在访问单个摄像头时 才支持景深功能。
当您使用单个摄像头时, 当用户打开或关闭 iPhone Duo 时, 您的应用需要负责在摄像头之间进行切换。 请使用新的 AVCaptureDeviceDirectionCoordinator API 来管理 此切换过程。
作为背景知识, AVCaptureDevice 一直 提供一个固定位置属性。 在 iPhone 上,该属性一直为“后”或“前”。 内屏和外屏的前置摄像头均取值为“前”。 但在 iPhone Duo 上情况则更为有趣, 因为其显示屏可能朝向相反方向。 这意味着前置摄像头并不总是对着你。 例如,你可能正在观看内屏, 但视频流却来自外屏的前置摄像头。 在这种情况下,前置摄像头是背对着你的。
当你的应用正在从外屏的前置 摄像头进行视频流传输时, 你可能会合上设备。 此时,同一台摄像头会转过来直接面对你。
如果你在设备打开时将其翻转, 就可以使用后置摄像头拍摄自拍照。 此时,你的应用和后置 摄像头都正对着你。
为了确保你的应用始终知道摄像头朝向何处, 请使用方向协调器。 方向协调器会告知你的应用 摄像头的位置,并在位置 发生变化时实时更新信息。
要创建方向协调器,你需要三样东西 :应用的 UIView、 你要监控的设备类型, 以及一个变更处理程序。
当应用的 UIView 位于外屏时, 协调器会将外屏的前置摄像头显示为“向前”, 后置摄像头显示为“向后”。 当设备展开时, 应用的 UIView 会移至内屏。 此时将调用您的变更处理程序, 该程序会将外屏的前置 和后置摄像头报告为“后置摄像头”, 并将内屏的前置摄像头报告为“前置摄像头”。 如果在设备展开状态下翻转设备, 您可以将相机应用切换至外屏。 此时,您会发现 后置摄像头和外屏的前置摄像头都 变成了“前置摄像头”。
在 iPhone Duo 上, 相机应用可以同时使用两个显示屏。 这非常适合进行群组视频通话, 或者在给孩子拍照时向他们展示有趣的内容。 在这种情况下, 每个显示屏将同时拥有 两个不同的 UIView。 一个用于外屏, 一个用于内屏。
为每个视图创建一个单独的方向协调器。 外屏的协调器会将后置 摄像头报告为前置摄像头, 而内屏的协调器则会将其 报告为后置摄像头。 每个协调器都会报告相对于自身视图的位置。 相机应用可以通过 SceneAccessories API 启用此功能。 请观看视频《 在 iPhone Duo 上利用多屏幕和场景》
了解具体方法。由于方向协调器与视图绑定, 因此它被隔离在主演员中。 请注意,来自方向协调器的变更处理程序不应直接 使用 AVFoundation API。
协调器不会直接提供 AVCaptureDevice, 而是提供一个 AVCaptureDeviceDescriptor。
设备描述符是 AVCaptureDevice 的主演 员安全且可发送的表示形式。
它包含创建 AVCaptureDevice 所需的所有信息, 并可安全地传递给您的摄像头演员。 在此基础上,您可以调用 AVFoundation API。 以下是在变更处理程序中应执行的操作:
当用户打开或关闭设备时, 系统会调用您的变更处理程序, 告知应用当前哪台摄像头处于前置状态。 此时,请重新配置您的 AVCaptureSession, 以保持前置摄像头的实时流传输。
请考虑新的摄像头方向如何影响 您对预览镜像的决策。 当后置摄像头作为前置摄像头使用时, 请将预览画面 镜像显示,以提供自然的自拍体验。 最后,利用变更处理程序在切换摄像头时 执行任何 UI 更新。 您可以在 AVKit 中找到方向协调器。 有关该协调器的更多详细信息, 请参阅 Apple 开发者文档中的文章 《根据摄像头朝向选择摄像头》。
既然我们已经介绍了相机方向的变更, 接下来我将分享一些技巧, 帮助您的应用在 iPhone Duo 上呈现出更精致的预览效果。 当您在内屏使用后置相机的全视场进行 流媒体传输时, 预览画面周围会有 额外的显示空间。 请充分利用这部分额外空间, 通过偏移预览位置, 以便在剩余空间中分组放置控件。 或者,您也可以让预览填满整个屏幕。 您可以根据应用需求灵活 选择最合适的方案。 使用 AVCaptureVideoPreviewLayer 上的 videoGravity 属性, 来确定预览在图层边界内的布局方式。 请查阅文档以获取更多详细信息。 从超广角前置摄像头进行直播时, 您可以利用方形 传感器让预览填满整个屏幕。 使用 AVCaptureDevice 上的 dynamicAspectRatio 属性,可在内屏上选择横向宽高比。
在 WWDC26 的视频 《在您的 iOS 应用中支持 Center Stage 前置 摄像头》中,了解有关正方形 传感器的更多强大功能。
关于在应用中处理旋转的最后 一点说明:采用旋转协调器, 以确保您的摄像头预览 和照片始终保持竖直。 在 iPhone Duo 上, 当应用在不同显示屏之间切换时, 旋转协调器会自动更新。 使用该协调器可确保应用的旋转设置 在各显示屏上保持一致。 请阅读《在相机应用中支持设备旋转》 一文,了解 有关使用旋转协调 器的更多信息。
采用旋转协调器后, 请禁用相机传感器 方向补偿功能以提升性能。 此补偿功能在 iPhone Duo 的所有 前置摄像头上均处于启用状态。
接下来需要采取的几个步骤。 使用 iOS 27.1 SDK 来充分利用 iPhone Duo 的功能。 确定当设备打开或关闭时, 您的应用将如何处理摄像头切换。 如果您希望突破虚拟 前置摄像头的限制,请采用方向协调器。 最后,在 iPhone Duo 上测试您的应用, 并确保摄像头预览效果出色。
我期待看到您的摄像头应用 在 iPhone Duo 上呈现的效果。
-
-
3:00 - Understand AVCaptureDevicePosition
enum AVCaptureDevicePosition: Int { case unspecified case back case front } extension AVCaptureDevice { // ... var position: AVCaptureDevicePosition { get } } -
4:06 - Initialize a direction coordinator
directionCoordinator = AVCaptureDeviceDirectionCoordinator( view: view, deviceTypes: [ .builtInOuterUltraWideCamera, .builtInInnerUltraWideCamera, .builtInDualWideCamera, ], changeHandler: { [weak self] map in self?.updateCameraSession(map) } ) -
7:30 - Configure the video preview layer
class AVCaptureVideoPreviewLayer { // ... var videoGravity: AVLayerVideoGravity { get set } } -
7:51 - Select a dynamic aspect ratio
class AVCaptureDevice { // ... var dynamicAspectRatio: AVCaptureDevice.AspectRatio? { get } } -
8:34 - Disable sensor orientation compensation
// Disable for improved performance class AVCapturePhotoOutput: AVCaptureOutput { // ... var isCameraSensorOrientationCompensationEnabled: Bool { get set } }
-
-
- 0:00 - Introduction
Tarun from the camera software team introduces the new camera features on iPhone Duo: the new front cameras, handling camera direction changes when people open or close the device, and tips for polishing your app's camera preview.
- 0:31 - Meet the new front cameras
iPhone Duo is the first iPhone with two front cameras, both square sensors with an ultrawide field of view. The outer ultrawide camera sits on the outside of the device, while opening it reveals the inner ultrawide camera — the first under-display camera on iPhone.
- 0:58 - Discover the virtual front camera
Stream from the front cameras through AVFoundation, using AVCaptureDeviceDiscoverySession with position .front and the wide or ultrawide device type. On iPhone Duo this discovers the new virtual front camera, which automatically switches between the inner physical camera when open and the outer one when closed, always using the most relevant camera for your app.
- 1:53 - Access each camera individually
Each front camera has its own AVCaptureDevice type — built-in outer ultrawide and built-in inner ultrawide — giving access to each camera's full capabilities. The inner camera supports 1080p up to 60fps, the outer up to 4K and 120fps. The virtual front camera exposes only features common to both, meaning 1080p and 60fps, and depth is supported only when accessing the individual cameras.
- 2:46 - Track camera direction
When using individual cameras, your app is responsible for switching as people open or close the device — use the new AVCaptureDeviceDirectionCoordinator. AVCaptureDevice's position property has always been back or front, and both front cameras report front, but on iPhone Duo the displays can face opposite directions, so a front camera isn't always looking at you. The coordinator keeps your app up to date on where cameras are facing.
- 4:03 - Create a direction coordinator
A direction coordinator needs three things: your app's UIView, the device types to monitor, and a change handler. When your view is on the outer display, outer front cameras report as forward-facing and rear cameras as backward-facing. Opening the device moves your view to the inner display and calls your change handler, which then reports the inner front camera as forward-facing.
- 4:50 - Use both displays at once
A camera app can use both displays simultaneously — useful for a group video call, or for showing a child something fun while photographing them. With a separate UIView per display, create a separate direction coordinator for each, since each reports positions relative to its own view. Camera apps enable this with the SceneAccessories API, covered in "Leverage multiple displays and scenes on iPhone Duo".
- 5:38 - Work with device descriptors
Because the direction coordinator is tied to a view, it's isolated to the main actor, so your change handler shouldn't use AVFoundation APIs directly. Instead of an AVCaptureDevice, the coordinator provides an AVCaptureDeviceDescriptor — a main-actor-safe, sendable representation containing everything needed to create an AVCaptureDevice, which you can safely pass to your camera actor.
- 6:17 - Handle a direction change
When your change handler is called, reconfigure your AVCaptureSession to keep streaming from the forward-facing camera. Consider how the new direction affects mirroring — mirror the preview when the rear camera is forward-facing for a natural selfie experience — and perform any UI updates your app needs. The direction coordinator is found in AVKit.
- 7:03 - Polish your camera preview
Streaming the rear camera's full field of view on the inner display leaves extra space around the preview: offset the preview to group controls in the remaining space, or let it fill the display. Use videoGravity on AVCaptureVideoPreviewLayer to control layout within the layer bounds. When streaming from the ultrawide front cameras, use dynamicAspectRatio on AVCaptureDevice to select a landscape aspect ratio and fill the display.
- 8:03 - Handle rotation
Adopt AVCaptureDeviceRotationCoordinator to keep your camera preview and photos upright. On iPhone Duo it updates when your app moves between displays, so rotation is applied consistently. After adopting it, disable camera sensor orientation compensation — enabled on all iPhone Duo front cameras — to improve performance.
- 8:47 - Next steps
Use the iOS 27.1 SDK to take full advantage of iPhone Duo, decide how your app will handle switching cameras when the device opens or closes, adopt a direction coordinator if you want to move beyond the virtual front camera, and test that your camera preview looks great.