为何canSetSessionPreset在设备不支持4K预设时始终返回true?
检测设备相机是否支持4K的正确方法
你当前使用的canSetSessionPreset(_:)方法无法准确判断设备是否支持4K,原因是该方法仅验证预设值本身是否合法,不关联具体的相机硬件——即使还未添加任何相机输入到会话,它也会返回true。
要准确检测设备是否支持4K拍摄,需要直接枚举设备的相机硬件,检查其支持的视频格式中是否包含4K分辨率:
实现步骤
- 遍历设备支持的相机类型(如广角、超广角、长焦)
- 对每个相机,获取其所有支持的
AVCaptureDeviceFormat - 检查格式的分辨率是否为4K标准(3840×2160 或 2160×3840,适配横竖屏方向)
Swift代码示例
import AVFoundation func is4KCameraSupported() -> Bool { // 要检测的相机类型 let targetDeviceTypes: [AVCaptureDevice.DeviceType] = [ .builtInWideAngleCamera, .builtInUltraWideCamera, .builtInTelephotoCamera ] for deviceType in targetDeviceTypes { // 获取对应类型的默认相机设备 guard let cameraDevice = AVCaptureDevice.default( deviceType, for: .video, position: .unspecified ) else { continue } // 遍历设备支持的所有格式 for format in cameraDevice.formats { let videoDimensions = format.formatDescription.dimensions // 检查是否符合4K分辨率(含横竖屏) let is4K = (videoDimensions.width == 3840 && videoDimensions.height == 2160) || (videoDimensions.width == 2160 && videoDimensions.height == 3840) if is4K { return true } } } return false } // 使用示例 if is4KCameraSupported() { print("4K supported") } else { print("4K unsupported") }
说明
- 该方法会检查设备上所有主流相机类型,只要其中一个支持4K就返回
true - 无需请求相机权限即可获取设备格式信息,不会触发权限提示
内容的提问来源于stack exchange,提问作者Usukhbayar Batbayar
相关产品推荐
相关产品推荐

