更新到 ios 8 后,我似乎无法获得 CMFormatDescriptionRefs Dimensions 它总是返回 0,0 维。已经看过 apples 文档,但我真的不明白如何应用它。
这是我在 ios 7 上使用的现有代码(已经在我的 ios 7 设备上测试过了)
NSArray *ports = [input ports];
AVCaptureInputPort *usePort = nil;
for ( AVCaptureInputPort *port in ports )
{
if ( usePort == nil || [port.mediaType isEqualToString:AVMediaTypeVideo] )
{
usePort = port;
break;
}
}
CMVideoFormatDescriptionRef format = [usePort formatDescription];
CMVideoDimensions dim = CMVideoFormatDescriptionGetDimensions(format);
cameraSize = CGSizeMake(dim.width, dim.height);
最佳答案
通过为 AVcapture 端口添加观察者解决了我的问题 formatDescription 因为在 ios 8 版本中,使用获取端口格式描述的旧方法为您提供了一个“0”值。
第一行为AVCapture添加了一个观察者
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(avCaptureInputPortFormatDescriptionDidChangeNotification:)
name:AVCaptureInputPortFormatDescriptionDidChangeNotification object:nil];`
如果 AVCapture 发生变化,下一行将为您提供端口描述
- (void)avCaptureInputPortFormatDescriptionDidChangeNotification:(NSNotification *)notification {
[self initializeAPI];
NSArray *ports = [deviceInput ports];
AVCaptureInputPort *usePort = nil;
for ( AVCaptureInputPort *port in ports )
{
if ( usePort == nil || [port.mediaType isEqualToString:AVMediaTypeVideo] )
{
usePort = port;
break;
}
}
CMFormatDescriptionRef formatDescription = usePort.formatDescription;
if (formatDescription) {
CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
if ((dimensions.width == 0) || (dimensions.height == 0)) {
return;
}
CGFloat aspect = (CGFloat)dimensions.width / (CGFloat)dimensions.height;
BOOL is7x = floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1;
if (is7x) {
}else{
aspect = 1.f / aspect;
dimensionForIOS8Above = CGSizeMake(dimensions.width, dimensions.height);
//NSLog(@"8DIMENSION %f",dimensionForIOS8Above.width);
}
}
关于ios - CMFormatDescriptionRef 和 CMVideoFormatDescriptionGetDimensions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26175409/