【问题标题】:extending UIImagePicker controller doesn't help to prevent rotation in io6扩展 UIImagePicker 控制器无助于防止 io6 中的旋转
【发布时间】:2013-01-21 18:21:36
【问题描述】:

我的应用在 info.plist 中设置为仅支持纵向模式。

但是,UIImagePickerController 会在用户将屏幕旋转为横向时旋转。

由于在 io6 中没有调用 shouldAutoRotate 方法,所以我尝试像这样扩展它:

@interface NonRotatingUIImagePickerController : UIImagePickerController

@end

@implementation NonRotatingUIImagePickerController

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait;
}

@end 

但这无济于事。知道为什么吗?

在日志中我看到上面的方法被调用了。 UIImagePickerController 最初以纵向显示,当用户旋转时 - 它也会旋转而不是保持纵向。

我在视图中这样设置图像选择器:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (!self.imagePickerController) {
        self.imagePickerController = [[NonRotatingUIImagePickerController alloc] init];
        self.imagePickerController.delegate = self;
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated{
   self.imagePickerController.showsCameraControls = NO;
   CGRect imagePickerControllerFrame = CGRectMake(0, topBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - topBar.frame.size.height - bottomBar.frame.size.height);
   self.imagePickerController.view.frame = imagePickerControllerFrame;
   self.imagePickerController.allowsEditing = YES;
   self.imagePickerController.view.clipsToBounds = YES;
  self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera
  [self.view.window addSubview:self.imagePickerController.view];
}

【问题讨论】:

  • 你能用代码证明你确实在使用这个类吗?
  • 完成。请注意,我将图像选择器添加为子视图

标签: objective-c cocoa-touch ios6 uiimagepickercontroller


【解决方案1】:
self.imagePickerController.view.frame = imagePickerControllerFrame;
// ...
[self.view.window addSubview:self.imagePickerController.view];

嗯,这完全是非法的。 Apple 在文档中非常清楚地说明了这一点:

此类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改

只有一种正确的方法可以使用使用 UIImagePickerControllerSourceTypeCamera 的图像选择器控制器 - 作为全屏呈现的视图控制器:

BOOL ok = [UIImagePickerController isSourceTypeAvailable:
           UIImagePickerControllerSourceTypeCamera];
if (!ok) {
    NSLog(@"no camera");
    return;
}
NSArray* arr = [UIImagePickerController availableMediaTypesForSourceType:
                UIImagePickerControllerSourceTypeCamera];
if ([arr indexOfObject:(NSString*)kUTTypeImage] == NSNotFound) {
    NSLog(@"no stills");
    return;
}
UIImagePickerController* picker = [UIImagePickerController new];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = @[(NSString*)kUTTypeImage];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];

如果您想在自己的界面呈现实时拍照界面,请使用 AVFoundation 和它为您提供的相机捕捉 API。

可在此处下载工作示例:

https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/ch30p816cameraCaptureWithAVFoundation/p683cameraCaptureWithAVFoundation/ViewController.m

【讨论】:

  • 我尝试将其创建为全屏 - 尽管我的应用在 info.plist 中仅针对纵向模式定义,但它仍会旋转。这是否意味着为了禁用旋转我必须使用 AVFoundation 接口?
  • 在 iPad 上,它是可旋转的,对此您无能为力。 (这可能是一个错误,但我不知道;你可以尝试提交一个错误。)但底线是:这不是你的观点。这不是您的视图控制器。按原样使用它或使用 AVFoundation 自行开发。
  • 谢谢。我不明白为什么苹果为 UIImagePickerController 提供了如此有限的实现
  • 提交错误请求增强。但不管你喜不喜欢,你都应该遵守文档。您当然不应该不遵守文档,然后抱怨事情运行不正常。
  • 谢谢。我正在使用您的代码 - 但如何防止相机旋转?我希望它只在人像模式下,只在人像模式下拍照
【解决方案2】:

也许你会认为这个答案没有帮助;但我会从 Apple 的文档中粘贴一个 sn-p:

重要提示:UIImagePickerController 类仅支持纵向模式。此类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改,但有一个例外。您可以将自定义视图分配给 cameraOverlayView 属性,并使用该视图来显示附加信息或管理相机界面和代码之间的交互。

UIImagePickerController Doc Link

很抱歉成为一个杀人狂。您应该寻找替代课程。快速搜索显示有很多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多