UIImagePickerControllerCameraDeviceFront每隔一段时间工作一次 [英] UIImagePickerControllerCameraDeviceFront works every other time

查看:76
本文介绍了UIImagePickerControllerCameraDeviceFront每隔一段时间工作一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与此处提出的现有问题非常相似. 我尝试了提出的解决方案,但对我而言不起作用

This question is very similar to an existing question asked here UIImagePickerControllerCameraDeviceFront only works every other time I tried the solution presented but it didn't work for me

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

I have a simplest of a project with two view controllers. In the blue one I am displaying a small UIView with a UIImagePickerController in it. NOTE: I am displaying front facing camera when app is launched.

我按下了下一个按钮,转到橙色视图控制器,当我按下后退按钮,回到蓝色视图控制器时,UIImagePickerController从前向后翻转.我想这是因为它认为自己很忙并且移至后凸轮.如果我在视图控制器之间来回移动,那么相机会一直向前,向后,向前,向后,向前,向后翻转...

I hit the next button and go to orange view controller and when I hit the back button and come back to blue view controller the UIImagePickerController flips from Front to rear. I guess the reason is that it thinks its busy and moves to the rear cam. If I keep moving back and forth between the view controllers the camera keeps flipping front, back, front, back, front, back...

这是我的代码和屏幕截图,我在做什么错?

Here is my code and screenshots, what am I doing wrong?

在我的* .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文件中(此代码仅在显示蓝色视图控制器时使用)

In my *.m file (This code is only used when blue colored view controller is displayed)

#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

推荐答案

这是一个非常简单的问题.我不知道为什么会发生这种情况,但是似乎UIImagePickerController旨在在每次需要时重新创建,而不是保留对其的任何引用,如果您考虑一下,这似乎是合乎逻辑的.基本上,您每次都需要重新创建和重新配置选择器.下面,我粘贴了一些代码以给出我的意思的图像.

This is a very simple issue. I don't know why this happens exactly, but it seems that UIImagePickerController was designed to recreated each time it's needed instead of keeping any reference to it, which seems logical if you think about it. Basically, you need to recreate and reconfigure your picker each time. Below I've pasted some code to give an image of what I mean.

简单的解决方案:

- (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;
}

这篇关于UIImagePickerControllerCameraDeviceFront每隔一段时间工作一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆