每次加载某个视图控制器时,我都会尝试打开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];
}
答案 0 :(得分:1)
在以下方法中编写上述代码
- (void)viewDidAppear:(BOOL)animated {
}
答案 1 :(得分:1)
您可能希望使用viewDidAppear:
代替viewDidLoad
。
答案 2 :(得分: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;
}
}
答案 3 :(得分:1)
如果你看viewController life cycle
,你可以注意到
viewDidLoad()仅在您第一次加载之后一次调用 当你再次切换到相同的viewController时,它不会调用 to viewDidLoad。而不是调用viewDidLoad,而是调用
那么你必须写你的 中的相机开启代码- (void) viewWillAppear:(BOOL)animated
。
- (void) viewWillAppear:(BOOL)animated
方法
现在每当&你推动或呈现你的viewController多少次打开相机。