You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 02:20:46