当前位置 : 主页 > 手机开发 > ios >

ios – 不建议使用devicesWithMediaType

来源:互联网 收集:自由互联 发布时间:2021-06-11
我有以下代码: NSArray * devices = [ AVCaptureDevice devicesWithMediaType: AVMediaTypeVideo ];// 2. Iterate through the device array and if a device is a camera, check if it's the one we want:for ( AVCaptureDevice * device in devices
我有以下代码:

NSArray * devices = [ AVCaptureDevice devicesWithMediaType: AVMediaTypeVideo ];

// 2. Iterate through the device array and if a device is a camera, check if it's the one we want:
for ( AVCaptureDevice * device in devices )
{
    if ( useFrontCamera && AVCaptureDevicePositionFront == [ device position ] )
    {
        // We asked for the front camera and got the front camera, now keep a pointer to it:
        m_camera = device;
    }
    else if ( !useFrontCamera && AVCaptureDevicePositionBack == [ device position ] )
    {
        // We asked for the back camera and here it is:
        m_camera = device;
    }
}

警告说不支持devicesWithMediaType,我应该使用AVCaptureDeviceDiscoverySession,我尝试了以下内容:

AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera] 
                                      mediaType:AVMediaTypeVideo 
                                       position:AVCaptureDevicePositionBack];
NSArray *captureDevices = [captureDeviceDiscoverySession devices];

但是设备阵列只有我的后置摄像头而不是我的前置摄像头,有什么帮助吗?

从 documentation起

Pass AVCaptureDevicePositionUnspecified to search for devices
regardless of position.

因此,使用AVCaptureDevicePositionUnspecified来查找所有捕获设备.

所以代码看起来像这样:

AVCaptureDeviceDiscoverySession *captureDeviceDiscoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera] 
                                      mediaType:AVMediaTypeVideo 
                                       position: AVCaptureDevicePositionUnspecified]; // here you pass AVCaptureDevicePositionUnspecified to find all capture devices

NSArray *captureDevices = [captureDeviceDiscoverySession devices];
网友评论