基于照片方向设置自动旋转和首选方向
我有一个VC,用户可以拍摄照片或从他们的库中选择一张照片。基于照片方向设置自动旋转和首选方向
拍摄/选择照片后,它将被传递到下一个VC上的UIImageView
。
我想显示这个视图控制器,只在图片在的方向。因此,如果用户在左侧横向拍摄照片(一旦他们选择“使用”),则下一个视图控制器应该在横向上加载该UIImageView
中的该图像。如果他们以纵向拍摄图像,视图控制器将以纵向加载。一旦VC以正确的方向加载,它就不应该移动(再旋转)。
这是我到目前为止已经试过,但不能让它工作,我如何期望:
#pragma mark -
#pragma mark - Auto Rotation Overrides
- (BOOL)shouldAutorotate {
if (self.photoImage != NULL) {
// Check that photoImage has an image
if (self.photoImage.imageOrientation == 1 || self.photoImage.imageOrientation == 0) {
// Landscape left = 0, Landscape rigt = 1
return YES;
} else {
// Portrait normal = 3, portait upside down = 2
return NO;
}
}
// Fallback to portrait
return NO;
}
-(NSUInteger)supportedInterfaceOrientations {
if (self.photoImage != NULL) {
// Check that photoImage has an image
if (self.photoImage.imageOrientation == 0) {
return UIInterfaceOrientationMaskLandscapeLeft;
} else if (self.photoImage.imageOrientation == 1) {
return UIInterfaceOrientationMaskLandscapeRight;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
// Fallback to Portrait
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
if (self.photoImage != NULL) {
// Check that photoImage has an image
if (self.photoImage.imageOrientation == 0) {
return UIInterfaceOrientationLandscapeLeft;
} else if (self.photoImage.imageOrientation == 1) {
return UIInterfaceOrientationLandscapeRight;
} else {
return UIInterfaceOrientationPortrait;
}
}
// Fallback to Portrait
return UIInterfaceOrientationPortrait;
}
----- -----编辑
由于shouldAutoRotate
不叫直到设备实际旋转,我怎么让它自动在viewDidLoad基于图像方向改变方向。我看到supportInterfaceOrientations
方法被调用,只有一个风景被返回,但不会立即移动视图...?
----- ----- EDIT2
是否在iOS6的preferredInterfaceOrientationForPresentation
工作吗?我可以设置我的导航控制器子类来查看self.topViewControllers preferredInterfaceOrientationForPresentation
,但是这似乎意味着每个topViewController都需要使用此方法。理想情况下,我想要设置子类以说: 如果topViewController对preferredInterfaceOrientationForPresentation
有响应,则使用其他纵向/默认值。这可以做到吗?
如果是这样,我当然可以更新preferredInterfaceOrientationForPresentation
这将加载该VC在正确的方向,然后shouldAutoRotate
将控制它不会被转回到另一个方向?
从我的理解,没有办法强制轮换事件,所以你必须与一些tricksy的东西来。我做了谷歌搜索“迫使一个UIView旋转”,并发现了一些链接,可以帮助:
Removing and re-adding the view from the window
Rotating the view with a transform
就个人而言,我会尝试变换第一,这里是有关的代码:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
self.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);
我不知道,如果设置状态栏方向将改变导航栏的方向,如果你使用的是一个......
我过去为这种情况所做的工作是为横向和纵向创建单独的viewControllers(例如我不希望视图自动旋转)。一旦你有你单独视图控制器,你可以看的UIImage的,像这样的尺寸:
if(sourceImage.size.width>sourceImage.size.height)
{
LandscapeViewController *LandscapeView = [[LandscapeViewController] init];
[self presentViewController:LandscapeView animated:YES completion:nil];
NSLog(@"go to Landscape View");
}
//Else load into portrait Edit view
else
{
PortraitViewController *PortraitView = [[PortraitViewController alloc] init];
[self presentViewController:PortraitView animated:YES completion:nil];
NSLog(@"go to Portrait View");
}
我敢肯定有更有效的办法,但是这是非常简单和工程!
这似乎不适用于我。我在故事板中创建了两个独立的VC,一个是Landscape和另一个端口。但视图总是先以人像加载。还有其他需要吗? – StuartM
从iOS 6开始,它是处理方向的navigationController。如您所想,您可以对其进行子类化,并使用respondsToSelector:
检查topviewcontroller是否具有所需的选择器。
- (NSUInteger)supportedInterfaceOrientations {
if([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)]){
return [self.topViewController supportedInterfaceOrientations];
}
return [super supportedInterfaceOrientations];
}
我的需要是强迫景观。我还添加在控制器的viewDidLoad
:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
您需要的方向改变为一个你想要的。
谢谢,是的,我正在使用导航栏。我想这应该应用在viewDidAppear基于我有的图像方向的if语句。 – StuartM
viewWillAppear,viewDidLoad或viewDidAppear应该取决于你的情况。 – JonahGabriel
感谢它旋转的意见,但不是你认为的导航栏...所以这不是一个解决方案的时刻。我的第二个想法是让prefferredInterface方法工作,而不是这样,它显示在正确的界面,并且shouldAutoRotate会让它再次旋转......?我将编辑与更新 – StuartM