ios - UIImagePicker 被选中后重新出现

标签 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];
}

我需要强制重新加载我的初始 View 吗?有没有更聪明的方法来做到这一点?

编辑

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

最佳答案

可能 viewWillAppear 每次 UIImagePickerController 消失时都会被调用 - 因为您放置代码的 View Controller 再次变得可见。

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

关于ios - UIImagePicker 被选中后重新出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31547683/

相关文章:

ios - UIView无限360度旋转动画?

ios - 转移到 swift 3 xcode 8 后,当我登录 Facebook 时没有任何反应

ios - 如何在 Titanium 中更改 App Id

ios - 异步加载的 UICollectionView 速度慢

ios - 在没有内存泄漏或类似方法的情况下将许多 UIImage 保存到 NSMutableArray

iphone - iOS 应用程序非视网膜和视网膜图像概念

ios - 分享App应用程序快捷方式【UIApplicationShortcutIconTypeShare】

objective-c - 何时保留 NSString?

ios - 将我的 iOS 更新到 15.1 后,我无法连接到蓝牙

ios - 在保留 alpha 的同时从阴影 View 创建 UIImage?