AVCaptureVideoDataOutput setVideoSettings:iOS10中不支持的像素格式类型

时间:2016-10-29 11:10:06

标签: ios ios10 ios-camera

self.videoOutput = AVCaptureVideoDataOutput() self.videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as AnyHashable : kCVPixelFormatType_32BGRA]

引发

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCaptureVideoDataOutput setVideoSettings:] Unsupported pixel format type - use -availableVideoCVPixelFormatTypes'

好的,所以我想检查可用的像素格式类型。

(lldb) po self.videoOutput.availableVideoCVPixelFormatTypes ▿ Optional<Array<Any>> ▿ some : 3 elements - 0 : 875704438 - 1 : 875704422 - 2 : 1111970369

这没什么用。幸运的是,在this answer中,有一个类别可以打印出NSNumbers中的pixelFormat名称。

(lldb) po (self.videoOutput.availableVideoCVPixelFormatTypes[2] as! NSNumber).descriptivePixelFormat() Optional<String> - some : "kCVPixelFormatType_32BGRA"

嗯,所以我猜32BGRA毕竟是支持的?所以我尝试直接从可用格式类型数组中获取值:

self.videoOutput.videoSettings = [ kCVPixelBufferPixelFormatTypeKey as AnyHashable : self.videoOutput.availableVideoCVPixelFormatTypes.first!]

self.videoOutput.videoSettings = [ kCVPixelBufferPixelFormatTypeKey as AnyHashable : self.videoOutput.availableVideoCVPixelFormatTypes.last!]

并且都抛出了同样的例外。

我没有第二台设备可以测试atm。但我记得一年前在iOS9和其他设备上使用相同的代码。

iOS 10,XCode 8,iPhone SE

1 个答案:

答案 0 :(得分:0)

@property (nonatomic,strong) AVCaptureVideoDataOutput *avCaptureVideoDataOutput;

BOOL supportsFullYUVRange = NO;
BOOL supportsY420 = NO;
    _avCaptureVideoDataOutput = [[AVCaptureVideoDataOutput alloc] init];

NSArray *supportedPixelFormats = _avCaptureVideoDataOutput.availableVideoCVPixelFormatTypes;

for (NSNumber *currentPixelFormat in supportedPixelFormats){
    if ([currentPixelFormat intValue] == kCVPixelFormatType_420YpCbCr8Planar)
    {
        supportsY420 = YES;
    }
}
NSLog(@"Support Y420 is %d",supportsY420);


for (NSNumber *currentPixelFormat in supportedPixelFormats){
    if ([currentPixelFormat intValue] == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)
    {
        supportsFullYUVRange = YES;
    }
}

NSLog(@“支持420f是%d”,supportsFullYUVRange);

我遇到了同样的问题,我使用iPhone SE和iPhone 7在xcode 8.2中运行iOS 10.2运行结果。

 Support Y420 is 0
 Support 420f is 1

所以...似乎Y420在iOS 10中不再可用了...你应该使用420f而不是......它的工作原理...... kCVPixelFormatType_32BGRA也能正常工作。

 if (supportsFullYUVRange){
    [_avCaptureVideoDataOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
}else{
    [_avCaptureVideoDataOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];

}