选择图像后,UIImagePicker会重新出现

时间:2015-07-21 19:13:18

标签: ios objective-c uiimage uiimagepickercontroller

我选择了一张图片后,我的UIImagePicker会再次出现。我的代码正在检查相机,然后根据模拟器中没有相机的事实显示UIImagePicker

- (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;
    }
    else {
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    self.imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage, nil];
    [self presentViewController:self.imagePicker animated:NO completion:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self clear];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imageView.image = chosenImage;
    [self dismissViewControllerAnimated:YES completion:nil];
}

我是否需要强制重新加载我的初始视图?有更聪明的方法吗?

修改

我在导航栏中添加了一个条形按钮,并在该动作上触发了我的代码。这解决了问题,似乎是一种更好的方法。

2 个答案:

答案 0 :(得分:2)

每当viewWillAppear消失时,可能会调用UIImagePickerController - 因为您放置代码的视图控制器再次变得可见。

您可以尝试在UIImagePickerController中创建viewDidLoad,或者使用布尔属性来检查它是否是第一次调用它。

答案 1 :(得分:1)

您的代码由视图控制器的viewWillAppear方法调用。

当它触发时,它会以模态方式显示视图控制器。当您关闭它时,您的视图控制器被揭开。调用它的viewWillAppear方法,显示一个新的图像选择器,循环继续......