我需要在用户进入ViewControllerA
时显示图像选择器,并在用户制作照片并按下“使用照片”按钮后停留在那里。
实际上当用户点击“使用照片”按钮时出现问题,在这种情况下,图像选择器会一次又一次地弹出。我知道这是因为viewWillAppear
,但viewDidLoad
也不是一个好选择,因为在这种情况下,图像选择器不会出现超过1次。
可能有人可以给我一些指导,如何在没有这个问题的情况下关闭图像选择器?我只想在用户每次进入视图控制器时显示它,并在用户选择图像后关闭它。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = YES;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
self.imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *choosenImage = info[UIImagePickerControllerEditedImage];
self.placeholderImg.image = choosenImage;
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:NO completion:nil];
[self.tabBarController setSelectedIndex:1];
}
答案 0 :(得分:0)
在您的视图控制器的.m文件中,您可以声明一个BOOL
,如果您已经提交了UIImagePickerController
,该@interface MyCustomViewController ()
{
BOOL imagePickerHasBeenPresented;
}
@end
会保持不变。
viewWillAppear
然后在你的- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (imagePickerHasBeenPresented == NO)
{
imagePickerHasBeenPresented = YES;
//Code to present imagePicker
}
}
方法中检查bool:
{{1}}