现在不可能从didFinishPickingMediaWithInfo方法中删除图像选择器?

时间:2016-03-25 07:52:37

标签: ios objective-c xcode7

这似乎是一个简单的问题,我已经看到类似的问题得到了解答,但答案却不起作用(至少不再有)。我在viewDidAppear中有以下代码:

UIImagePickerController *imagePicker;
    imagePicker =[[UIImagePickerController alloc] init];
    imagePicker.allowsEditing = YES;

    {imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;}
    imagePicker.delegate=self;

    NSLog(@"ViewDidAppear");
        [self presentModalViewController:imagePicker animated:YES];
        NSLog (@"Modal Window Displayed.");}

这允许用户从图像库中选择一个有效的图像。一旦选择了该图像,就会调用didFinishPickingMediaWithInfo,并在其顶部显示此代码:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{

    NSLog(@"Image Is Picked");
    [[picker presentingViewController] dismissViewControllerAnimated:YES completion:nil];
    [self dismissModalViewControllerAnimated:YES];
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
    [self dismissViewControllerAnimated:NO completion:nil];
    [self.navigationController popViewControllerAnimated:YES];

正如您所看到的,我已经在StackOverflow上尝试了几种不同的方法来实现这一点。他们都被忽略了。没有错误,没有任何反应;挑选者的观点永远不会消失。是什么赋予了?我正在使用xCode 7.2.1。

我的代码中有类似的地方,我提出另一个模态视图,也无法摆脱它。

4 个答案:

答案 0 :(得分:0)

如果您使用presentViewController代替presentModalViewController

[self presentViewController:imagePicker animated: YES];

然后

[self dismissViewControllerAnimated:YES completion:nil];

应该有用 - 或许你同时调用的不同功能的组合会使事情变得混乱。

如果这不起作用,或许尝试使用dispatch_after略微延迟 - 可能会在viewDidAppear内提出问题导致问题:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.001 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) { 
    [self presentViewController:imagePicker animated: YES];
}

答案 1 :(得分:0)

下面的代码非常适合我。希望能帮助到你。 在class.h中

@interface class : UIViewController<UIImagePickerControllerDelegate>
{
    UIImagePickerController *picker;
}
调用getPhoto时,在class.m中的

,PickerViewController呈现。

- (IBAction)getPhoto:(UIButton *)sender {
    picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;     
    [self presentViewController:picker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker2 didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [picker2 dismissViewControllerAnimated:YES completion:nil];
}

答案 2 :(得分:0)

问题是didFinishPickingMediaWithInfo方法实际上没有关闭另一个视图,直到它执行完毕(即使在该方法中运行的内容花了将近一分钟!)。到方法完成时,我已经以模态方式呈现了另一个viewcontroller。几乎所有我一直在尝试的技术实际上仍然有效。

答案 3 :(得分:0)

在全球范围内声明您的imagePicker,如下所示

@implementation ViewController{
    UIImagePickerController *imagePicker;
}

显示全局声明的图像选择器

- (IBAction)chooosePic:(id)sender {   
    imagePicker =[[UIImagePickerController alloc] init];
    imagePicker.allowsEditing = YES;     {imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;}
    imagePicker.delegate=self;
    NSLog(@"ViewDidAppear");
    [self presentModalViewController:imagePicker animated:YES];
    NSLog (@"Modal Window Displayed.");
}

didFinishPickingMediaWithInfo方法更新如下:

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

将工作