Hey everyone,
I'm having a few problems with rotating a video feed. I'm using AVCaptureMetadataOutput to detect faces, and everything works fine while the device is in portrait orientation. However, when I rotate my device, obviously it doesn't work because the video feed thinks that I've turned sideways (I'm tracking yaw and pitch, so I need the correct orientation). So, I grab the AVCapture connection from my AVCaptureMetadataOutput, and set the videoOrientation property (all this code is below). However, this AVCaptureConnection for some reason always says it doesn't support setting this. Is there any way I can get it to accept changes to the device orientation? Here's my code.
-(void) orientationChanged {
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
AVCaptureVideoOrientation newOrientation;
if (deviceOrientation == UIDeviceOrientationPortrait){
newOrientation = AVCaptureVideoOrientationPortrait;
}
else if (deviceOrientation == UIDeviceOrientationPortraitUpsideDown){
newOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
}
else if (deviceOrientation == UIDeviceOrientationLandscapeLeft){
newOrientation = AVCaptureVideoOrientationLandscapeRight;
}
else if (deviceOrientation == UIDeviceOrientationLandscapeRight){
newOrientation = AVCaptureVideoOrientationLandscapeLeft;
}
else if (deviceOrientation == UIDeviceOrientationUnknown){
newOrientation = AVCaptureVideoOrientationPortrait;
}
else{
newOrientation = AVCaptureVideoOrientationPortrait;
}
AVCaptureConnection *metaConnection = [[self.metadataOutput connections] objectAtIndex:0];
if (metaConnection.supportsVideoOrientation) {
metaConnection.videoOrientation = newOrientation;
NSLog(@"Set orientation");
}
else {
NSLog(@"Cannot set orientation");
}
[self.captureVideoPreviewLayer.connection setVideoOrientation:newOrientation];
}I know this code is working correctly because my preview layer updates fine. self.metadataOutput is initialized as follows:
self.metadataOutput = [AVCaptureMetadataOutput new];
[self.metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[self.captureSession addOutput:self.metadataOutput];
self.metadataOutput.metadataObjectTypes = @[ AVMetadataObjectTypeFace ];Thanks!