我正在使用UIPopoverController中的UIImagePickerController,它在iOS 6上运行完美。但在iOS 7上,“预览”图片会旋转,但是如果我拍照,照片保存是正确的。
这是我获取picker的方式:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;
并将其添加到弹出控制器:
self.imagePickerPopOver = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[self.imagePickerPopOver presentPopoverFromRect:CGRectMake(aPosViewA.x, cameraButton_y, 100.0, 30.0) inView:self.detailViewController.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
这些是用于UIScrollView上按钮位置的计算,以便在正确位置显示弹出窗口:
presentPopoverFromRect:CGRectMake(aPosViewA.x, cameraButton_y, 100.0, 30.0)
我认为问题不在那里,因为我已经尝试了几种组合。
我也尝试在全屏模式下拍摄图像,但是应用程序只允许使用横向模式。如果以纵向模式拍摄图像并关闭模态视图,则应用程序也会保持在纵向模式。我找不到一种方法来防止UIImagePickerController切换到纵向模式,或者在模态视图关闭后强制应用程序返回横向模式。
更新
我从这里获取了答案并且更进一步。
在创建选择器并显示弹出窗口之前,我对视图进行了转换:
switch ([UIApplication sharedApplication].statusBarOrientation) {
case UIInterfaceOrientationLandscapeLeft:
self.imagePicker.view.transform = CGAffineTransformMakeRotation(M_PI/2);
break;
case UIInterfaceOrientationLandscapeRight:
self.imagePicker.view.transform = CGAffineTransformMakeRotation(-M_PI/2);
break;
default:
break;
}
只要我不转动iPad,这种方法就能正常工作。为此,我正在注册方向改变事件:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
并更改选择器视图:
- (void)orientationChanged:(NSNotification *)notification{
if (self.imagePicker) {
switch ([UIApplication sharedApplication].statusBarOrientation) {
case UIInterfaceOrientationLandscapeLeft:
self.imagePicker.view.transform = CGAffineTransformMakeRotation(M_PI/2);
break;
case UIInterfaceOrientationLandscapeRight:
self.imagePicker.view.transform = CGAffineTransformMakeRotation(-M_PI/2);
break;
default:
break;
}
}
}
剩余问题: 一开始我写过,当照片被拍摄时,它会正确地显示以接受或拒绝它。现在这也被转换了。不知何故,我需要知道图片何时被拍摄并将其转换回来。
而且,这真的是一个令人讨厌的hack,可能在下一个iOS更新中无法正常工作。有人有想法如何以更干净的方式实现它吗?
更新2:
这太恶心了,我找到了一个更干净的解决方案,解决了我的问题,但不是关于在弹出窗口控制器中使用图像选择器的初始问题的答案,这不被苹果推荐,但允许使用。
我现在已经对UIImagePickerController进行了子类化,如下所示:
@implementation QPImagePickerController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
@end
我正在使用全屏的图像选择器而非在一个弹出框里。目前已在iOS7上进行测试。