UIImagePickerControllerCameraDeviceFront每隔一段时间工作

时间:2013-01-30 21:46:59

标签: iphone ios uiimagepickercontroller

此问题与此处提出的现有问题非常相似UIImagePickerControllerCameraDeviceFront only works every other time我尝试了所提出的解决方案,但它对我不起作用

我有一个最简单的项目,有两个视图控制器。在蓝色的一个中,我正在显示一个带有UIImagePickerController的小UIView。注意:我在启动应用程序时显示前置摄像头。

我点击下一个按钮然后转到橙色视图控制器,当我点击后退按钮并返回到蓝色视图控制器时,UIImagePickerController从前向后翻转。我想原因是它认为它很忙并且移动到后凸轮。如果我继续在视图控制器之间来回移动,则相机会保持前后翻转,前后,前后,后翻...

这是我的代码和截图,我做错了什么?

在我的* .h

#import <UIKit/UIKit.h>

@interface v1ViewController : UIViewController <UIImagePickerControllerDelegate>
{
    UIImagePickerController *picpicker;
    UIView *controllerView;

}

@property (nonatomic, retain) UIImagePickerController *picpicker;
@property (nonatomic, retain) UIView *controllerView;

@end

在我的* .m文件中(此代码仅在显示蓝色视图控制器时使用)

#import "v1ViewController.h"
#import <MobileCoreServices/UTCoreTypes.h>

@implementation v1ViewController

@synthesize picpicker;
@synthesize controllerView;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];


    picpicker = [[UIImagePickerController alloc] init];

    picpicker.delegate = self;
    picpicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
    picpicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picpicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
    picpicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    picpicker.showsCameraControls = NO;
    picpicker.navigationBarHidden = NO;
    picpicker.wantsFullScreenLayout = NO;

    controllerView = picpicker.view;
    [controllerView setFrame:CGRectMake(35, 31, 250, 250)];

    controllerView.alpha = 0.0;
    controllerView.transform = CGAffineTransformMakeScale(1.0, 1.0);

    [self.view addSubview:controllerView];

    [UIView animateWithDuration:0.3
                          delay:0.0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         controllerView.alpha = 1.0;
                     }
                     completion:nil
     ];

}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [picpicker dismissModalViewControllerAnimated:YES];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    [picpicker dismissModalViewControllerAnimated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

@end

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:0)

您正在viewDidDisappear和viewWillDisappear方法中解除控制器。 这可能是你问题的原因。

答案 1 :(得分:0)

虽然我现在还没有带摄像头的设备来验证这一点,但似乎你并没有正确地解雇pickerview控制器。文档说明您应该在控制器上调用dismissModalViewControllerAnimated:以解除选择器(但是,对呈现的控制器的调用将传播给演示者 - 所以这不是问题),< strong>但在您的情况下,您不是首先以模态方式显示控制器,因此它将无法工作。

在这种情况下,我会尝试release选择器(如果不是在ARC下)并将其设置为nil(而不是调用[picpicker dismissModalViewControllerAnimated:YES];)。

PS。事实上,您的设计似乎存在更大的问题。由于每个按钮都设置为以模态方式呈现另一方<​​strong>,因此您不会解雇任何控制器。控制器只是相互堆叠。您应该考虑将它们嵌入到导航控制器中并让它处理层次结构,或者只是将dismissModalViewControllerAnimated:(iOS5 +上的dismissViewControllerAnimated:completion:)设置为第二个控制器按钮的操作而不是模态segue。

答案 2 :(得分:0)

这是一个非常简单的问题。我不知道为什么会发生这种情况,但似乎UIImagePickerController被设计为每次需要时重新创建而不是保留对它的任何引用,如果你考虑它,这似乎是合乎逻辑的。基本上,您需要每次都重新创建和重新配置选择器。下面我贴了一些代码来说明我的意思。

简单的解决方案:

- (UIImagePickerController *)loadImagePicker {
    UIImagePickerController *picpicker = [[UIImagePickerController alloc] init];
    picpicker.delegate = self;
    picpicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
    picpicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picpicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
    picpicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    picpicker.showsCameraControls = NO;
    picpicker.navigationBarHidden = NO;
    picpicker.wantsFullScreenLayout = NO;
    return picpicker;
}

并在:

-(void)viewWillAppear:(BOOL)animated{
    if(!self.picpicker){
        self.picpicker = [self loadImagePicker];
        [self.view addSubview: self.picpicker];
    }
}

-(void)viewWillDisappear:(BOOL)animated {
     [super viewWillDisappear:animated];
     [self.picpicker removeFromSuperview];
     self.picpicker = nil;
}