Objective c UIImagePickerController(使用摄像头作为源)在iPad2上不自动旋转,如何停止?
我正在尝试编写一个带有摄像头功能的应用程序,我使用覆盖视图来用图像装饰它 以下是我实现应用程序的方式: 我使用UIImagePickerController向用户显示相机拍摄的内容,并将UIImageView作为子视图添加到cameraOverlayView中,使其工作方式如下:Objective c UIImagePickerController(使用摄像头作为源)在iPad2上不自动旋转,如何停止?,objective-c,uiimagepickercontroller,landscape,autorotate,ipad-2,Objective C,Uiimagepickercontroller,Landscape,Autorotate,Ipad 2,我正在尝试编写一个带有摄像头功能的应用程序,我使用覆盖视图来用图像装饰它 以下是我实现应用程序的方式: 我使用UIImagePickerController向用户显示相机拍摄的内容,并将UIImageView作为子视图添加到cameraOverlayView中,使其工作方式如下: (图片位于) 在iPad2安装到位之前,这一切都很正常。。。它会像这样自动旋转并破坏布局: (图片位于) UIImagePickerController在iphone、ipod touch或原始iPad上从不旋转,但在
(图片位于) 在iPad2安装到位之前,这一切都很正常。。。它会像这样自动旋转并破坏布局:
(图片位于) UIImagePickerController在iphone、ipod touch或原始iPad上从不旋转,但在iPad2上旋转。 UIImagePickerContrller的类引用表示它“仅支持纵向模式”,但实际情况是它会像这样自动旋转……
有没有办法禁用自动旋转功能?
我尝试在显示UIImagePickerController的视图控制器的shouldAutorotateToInterfaceOrientation:方法中返回NO,但它仍然旋转
提前感谢。由于UIImagePickerController源于UIViewController的UINavigationController,您可以在UIViewController文档中的“”处查看该信息是否有用。您可以通过漫游UIImagePickerController的覆盖视图来补偿ipad的旋转。首先,您必须使用以下方式捕获通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationCallback:) name:nil object:nil];
然后使用以下代码:
- (void) notificationCallback:(NSNotification *) notification {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if ([[notification name] isEqualToString:@"UIDeviceOrientationDidChangeNotification"]) {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
switch ( orientation ) {
case UIInterfaceOrientationLandscapeRight:
NSLog(@"LandcapeRight");
[UIView beginAnimations:@"LandscapeRight" context:UIGraphicsGetCurrentContext()];
[UIView setAnimationDuration:0.4];
m_uiCameraOverlayView.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
break;
case UIInterfaceOrientationLandscapeLeft:
NSLog(@"LandscapeLeft");
[UIView beginAnimations:@"LandcapeLeft" context:UIGraphicsGetCurrentContext()];
[UIView setAnimationDuration:0.4];
m_uiCameraOverlayView.transform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(M_PI), 0, 0);
[UIView commitAnimations];
break;
case UIInterfaceOrientationPortraitUpsideDown:
NSLog(@"UpsideDown");
[UIView beginAnimations:@"UpsideDown" context:UIGraphicsGetCurrentContext()];
[UIView setAnimationDuration:0.4];
m_uiCameraOverlayView.transform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(-M_PI / 2), -128, -128);
[UIView commitAnimations];
break;
case UIInterfaceOrientationPortrait:
NSLog(@"Portrait");
[UIView beginAnimations:@"Portrait" context:UIGraphicsGetCurrentContext()];
[UIView setAnimationDuration:0.4];
m_uiCameraOverlayView.transform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(M_PI / 2), 128, 128);
[UIView commitAnimations];
break;
default:
NSLog(@"????");
break;
}
}
}
}
可以将叠加视图添加到窗口中,并将window.superview设置为cameraOverlayView。 关闭ModalController时,可以从窗口中删除覆盖视图 根据应用程序的结构,此解决方案可能会有点棘手
YourAppDelegate *appDelegate = (YourAppDelegate *) [[UIApplication sharedApplication] delegate];
[appDelegate.window addSubview:overlayView];
imagePickerController.cameraOverlayView = appDelegate.window.superview;
//When dismissing the UIImagePicker
[self dismissModalViewControllerAnimated:YES];
[OverlayView removeFromSuperview];
当您这样做时,您会注意到:
UIImagePickerController *camera = [UIImagePickerController new];
NSLog([self.camera shouldAutorotate] ? @"YES" : @"NO");
结果将是肯定的。我认为默认情况下,它被设置为YES
您可以将UIImagePickerController子类化,并添加此方法以覆盖该方法:
- (BOOL)shouldAutorotate{
return NO;
}
然后,不要使用UIImagePickerController,而是使用创建的子类
UIImagePickerSubclass *camera = [UIImagePickerSubclass new];
希望这有帮助:)我尝试过对UIImagePickerController进行子类化,并重写shouldAutorotateToInterfaceOrientation:方法,以便仅当它是纵向模式时才返回YES。。。但它不起作用,调用该方法时,该方法会将某些内容打印到控制台,我发现该方法从未被调用过。。。。你知道为什么会这样吗?你找到解决办法了吗?我做了一些类似于MarcVivet建议的事情,制作一个反向动画来对抗自转。我想我稍后会尝试你的解决方案,因为这样做似乎更整洁(我现在没有ipad2可以尝试)。这就是我现在正在做的,尽管代码很长,但它对我来说很管用。我没有使用通知中心,而是在willRotateToInterfaceOrientation:duration:方法中实现了代码,这样我就可以准确地知道进行轮换所需的时间。我根据您的建议和Eric的建议尝试了这一点,但两种方法都不起作用。在iPad 2上,我收到了很多UIDeviceOrientationIDChangeNotifications,每次旋转时至少有2个,而且旋转始终无法正常进行。此代码仅适用于横向,如果您想使用纵向模式,则必须更改转换。。。我从没想过这个。。。如果我在窗口上添加子视图,它是否永远不会旋转(因为窗口不旋转)?此解决方案对我有效,但上面的MarcVivet没有。谢谢