I am currently developing an application to record and edit videos. The hole application supports only landscape mode - so I would like to use the camera controls in landscape to. But the preview screen always switches back to portrait. A similar question was asked before (camera preview is portrait in landscape app) but sadly without any accepted answer.
I tried several approaches:
First I added the view of the UIImagePickerController
manually - the preview was exactly as I want it to be, but the video image (while recording) was wrong (turned sideways).
code:
- (BOOL) startCameraController {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO) {
return NO;
}
self.cameraUI = [[UIImagePickerController alloc] init];
self.cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
self.cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
self.cameraUI.allowsEditing = NO;
self.cameraUI.delegate = self.parentViewController;
[self.parentViewController addChildViewController:self.cameraUI];
self.cameraUI.view.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[self.parentViewController.view addSubview:self.cameraUI.view];
return YES;
}
result:
Then I used presentViewController
to show the UIImagePickerController
- the camera recording works perfectly fine, but the preview screen is in portrait and I have to turn my device to work with it.
code:
- (BOOL) startCameraController {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO) {
return NO;
}
self.cameraUI = [[UIImagePickerController alloc] init];
self.cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
self.cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
self.cameraUI.allowsEditing = NO;
self.cameraUI.delegate = self.parentViewController;
[self.parentViewController presentViewController:self.cameraUI animated:YES completion:^{}];
return YES;
}
result:
I even listend to the event _UIImagePickerControllerUserDidCaptureItem
to detect when the camera changed to preview and tried to manually turn the view - but I wasn't able to adapt the frame dimensions of the preview view.
code:
- (void) cameraChangedToPreview {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight) {
self.cameraUI.view.transform = CGAffineTransformMakeRotation(M_PI_2);
} else if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft) {
self.cameraUI.view.transform = CGAffineTransformMakeRotation(-M_PI_2);
}
if (IS_WIDESCREEN) {
self.cameraUI.view.frame = CGRectMake(0, 0, 568, 320);
} else {
self.cameraUI.view.frame = CGRectMake(0, 0, 480, 320);
}
}
result:
I would be really grateful for any hints or suggestions how I could use camera and preview in landscape mode, so that I can use my hole application without turning the device in the process (The device will be in a tripod while the application is used, so it can't be turned.)
Thank you and best wishes!