繁体   English   中英

每次视图控制器处于活动状态时,我都会尝试打开相机

[英]I am trying to open a camera every time view controller is active

我每次加载某个视图控制器时都试图打开iPhone摄像头。 然而,它只打开一次,如果我再次加载视图控制器,它不会打开相机。 每次进入视图控制器时如何打开相机?

  - (void)viewDidLoad {
[super viewDidLoad];


  UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
    [imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
}

// image picker needs a delegate,
[imagePickerController setDelegate:self];

// Place image picker on the screen
[self presentModalViewController:imagePickerController animated:YES];
}

在以下方法中写上面的代码

- (void)viewDidAppear:(BOOL)animated {

}

您可能希望使用viewDidAppear:而不是viewDidLoad

写下面的代码:

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

    self.actionsheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Take Photo with Camera", @"Select from Library", nil];
    [self.actionsheet showInView:self.view];

  self.imgPicker=[[UIImagePickerController alloc] init];
  self.imgPicker.delegate=self;
  self.imgPicker.wantsFullScreenLayout = YES;
  self.imgPicker.allowsEditing=YES;

}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0)
    {
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
            self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        else
            self.imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        [self presentViewController:self.imgPicker animated:YES completion:nil ];

        self.btnBlure.tag = 102;
    }
    else if (buttonIndex==1)
    {
        self.imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        [self presentViewController:self.imgPicker animated:YES completion:nil ];

        self.btnBlure.tag = 103;
    }
}

如果你看一下viewController life cycle ,你可以注意到

viewDidLoad()只在第一次加载时调用,之后当你再次切换到同一个viewController时它不会调用viewDidLoad。 而不是调用viewDidLoad,它调用- (void) viewWillAppear:(BOOL)animated

然后你必须写下你的相机开场代码

- (void) viewWillAppear:(BOOL)animated方法

现在无论何时推送或呈现viewController,它都会打开相机。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM