1

每次加载某个视图控制器时,我都会尝试打开 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];
}
4

4 回答 4

1

用下面的方法写上面的代码

- (void)viewDidAppear:(BOOL)animated {

}
于 2013-01-26T09:58:00.490 回答
1

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

于 2013-01-26T09:58:20.197 回答
1

编写以下代码:

- (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;
    }
}
于 2013-01-26T09:59:27.140 回答
1

如果你看viewController life cycle,你会注意到

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

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

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

现在,无论何时以及多少次您推送或展示您的 viewController,它都会打开相机。

于 2013-01-26T10:56:39.890 回答