我正在开发一个iPhone应用程序,我必须使用AVCaptureSession从后置摄像头捕获图像。每次应用程序启动时,它将从服务器接收2个值,第一个值是AVCaptureSession的 sessionPreset 属性,第二个值是用于剪切输出图像的GCRect(此矩形的坐标对应到输出图像的分辨率)。
现在的问题是,我必须将剪切矩形“绘制”到相机预览上,这意味着我必须将剪裁矩形的坐标映射到相机预览的另一个“正确”坐标(因为sessionPreset)每次申请开始时都会有所不同)。我搜索了Apple的文档,但我找不到获得输出图像分辨率的方法。我需要输出分辨率才能进行剪切矩形的映射。
这是我在视图控制器中启动AVCaptureSession的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
//Capture Session
AVCaptureSession *session = [[AVCaptureSession alloc]init];
session.sessionPreset = _sessionPreset; //this _sessionPreset varies each time my app starts
//Add device
AVCaptureDevice *device =
[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
//Input
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
if (!input)
{
NSLog(@"No Input");
}
[session addInput:input];
//Output
AVCaptureStillImageOutput *output = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil];
[output setOutputSettings:outputSettings];
[session addOutput:output];
//Preview Layer
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
UIView *myView = self.view;
previewLayer.frame = myView.bounds;
previewLayer.videoGravity = AVLayerVideoGravityResizeAspect;
[self.view.layer addSublayer:previewLayer];
//Start capture session
[session startRunning];
//Now I want to draw the rectangle after the session starts
}
先谢谢费拉斯:)
答案 0 :(得分:2)
幸运的是,我在这里找到了我的问题的答案:
iOS8 video dimension, CMVideoDimensions returns 0,0
确保在[captureSession startRunning];
之后捕获通知