1

这个问题与此处提出的现有问题非常相似UIImagePickerControllerCameraDeviceFront 仅在我尝试提出的解决方案时每隔一次才有效,但它对我不起作用

我有一个最简单的项目,有两个视图控制器。在蓝色中,我正在显示一个带有 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

在此处输入图像描述

在此处输入图像描述

4

3 回答 3

0

尽管我现在没有可用的带摄像头的设备来验证这一点,但您似乎没有正确关闭pickerview 控制器。该文档指出,您应该调用dismissModalViewControllerAnimated:控制器以关闭选择器(尽管,对呈现控制器的调用将传播给演示者 - 所以这不是问题),在您的情况下,您没有以模态方式显示控制器第一个地方,所以它不会工作。

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

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

于 2013-02-01T09:02:23.747 回答
0

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

于 2013-01-30T23:31:58.720 回答
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;
}
于 2013-02-12T11:59:33.767 回答