【问题标题】:UIImagePickerController - [picker dismissViewControllerAnimated:YES completion:Nil]; ErrorUIImagePickerController - [picker dismissViewControllerAnimated:YES 完成:Nil];错误
【发布时间】:2013-05-23 10:24:34
【问题描述】:

过去 2 天我一直在尝试这个,但我无法找出答案。我找遍了,没有找到答案。

问题是我有一个按钮,可以在我的应用程序中调出相机(仅用于拍照)。相机打开了,但是当我拍照并点击“使用”(which is displayed at the bottom right) 时它崩溃了。另外,当相机打开时,在我点击"Cancel"拍照之前它再次崩溃。

我尝试使用断点并发现,当我点击“USE”按钮时,它会在这一行崩溃

[picker dismissViewControllerAnimated:YES completion:Nil]

我正在我的 iPad (iOS6) 上对其进行测试。

这里是按钮代码

-(IBAction)getAlbum:(id)sender {

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        NSArray *media = [UIImagePickerController
                          availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];

        if ([media containsObject:(NSString*)kUTTypeImage] == YES) {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            //picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
            [picker setMediaTypes:[NSArray arrayWithObject:(NSString *)kUTTypeImage]];

            picker.delegate = self;
            //[self presentModalViewController:picker animated:YES]; //Since [Modal](http://stackoverflow.com/questions/12445190/dismissmodalviewcontrolleranimated-deprecated) has been removed
            [self presentViewController:picker animated:YES completion:Nil];
            //[picker release];

        }
        else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unsupported!"
                                                            message:@"Camera does not support photo capturing."
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
        }

    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unavailable!"
                                                        message:@"This device does not have a camera."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }


}

imagePickerController 方法在这里:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSLog(@"Media Info: %@", info);
    NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];

    if([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
        UIImage *photoTaken = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

        //Save Photo to library only if it wasnt already saved i.e. its just been taken
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            UIImageWriteToSavedPhotosAlbum(photoTaken, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        }


        selectedLogoImg.image=photoTaken; //selectedLogoImg is the imageView
        [self.clipartItemView addSubview:selectedLogoImg]; // To detect touch and move it I place it as a subview of self.clipartItemView
    }

    //[picker dismissModalViewControllerAnimated:YES];
    [picker dismissViewControllerAnimated:YES completion:Nil]
    //[picker release];
    //[picker dismissViewControllerAnimated:YES completion:^{
      //  NSLog(@"Dismiss completed");
    //}];
}

didFinishSavingWithError 代码在这里:

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    UIAlertView *alert;
    //NSLog(@"Image:%@", image);
    if (error) {
        alert = [[UIAlertView alloc] initWithTitle:@"Error!"
                                           message:[error localizedDescription]
                                          delegate:nil
                                 cancelButtonTitle:@"OK"
                                 otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

}

imagePickerControllerDidCancel 代码在这里:

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    //[picker dismissModalViewControllerAnimated:YES];
    /*[picker dismissViewControllerAnimated:YES completion:^{
        [self.view sendSubviewToBack:cardGalleryView];
    }];*/
    [picker dismissViewControllerAnimated:YES completion:Nil];

}

【问题讨论】:

    标签: ios xcode ipad uiimagepickercontroller


    【解决方案1】:

    您应该将dismissViewControllerAnimated:completion: 消息发送到视图控制器,而不是选择器。试试:

    [self dismissViewControllerAnimated:YES completion:nil];
    

    以上方法仅适用于iOS 6,iOS 5及以下需要使用[self dismissModalViewControllerAnimated:YES]

    看一下文档中对方法的描述:

    关闭接收者提供的视图控制器。

    呈现视图控制器负责关闭视图 它呈现的控制器。如果您在呈现的视图上调用此方法 控制器本身,但是,它会自动将消息转发到 呈现视图控制器

    【讨论】:

    • 我应该在 imagePickerControllerDidCancel 或 imagePickerController 方法中这样做吗?
    • @Vikr 在您尝试关闭选择器的每一行中,基本上是在现有代码中每次出现 [picker dismissViewControllerAnimated:YES completion:Nil];
    • dismissModalViewControllerAnimated 在 ios6 中已弃用,他们建议使用 dismissViewControllerAnimated ?
    • @Vikr 不,他们建议使用dismissViewControllerAnimated:completion:,区别在于新添加的完成方法。
    • 你想让我使用 [self dismissModalViewControllerAnimated:YES completion:nil]; - 就像你建议的那样或 [self dismissViewControllerAnimated:YES completion:Nil];
    【解决方案2】:

    这个问题是由于您的 UIImagePickerController *picker 对象造成的。 ViewController 无法在 getAlbum 方法范围之外识别您的选取器对象引用。

    1.> 您可以在您的.h 文件中创建UIImagePickerController 对象

     @interface yourViewController : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate,UINavigationControllerDelegate,UIPopoverControllerDelegate>
    {
    
       UIImagePickerController *picker;
       UIPopoverController *popover;
    } 
    

    .m 文件中,您只需在getAlbum IBAction 方法中使用它

    -(IBAction)getAlbum:(id)sender {
    
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        NSArray *media = [UIImagePickerController
                          availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
    
        if ([media containsObject:(NSString*)kUTTypeImage] == YES) {
    
             UIButton *btn=(UIButton *)sender;
             if ([popover isPopoverVisible])
             {
                  [popover dismissPopoverAnimated:YES];
                  popover=nil;
             }
             picker = [[UIImagePickerController alloc]init];
             picker.sourceType = UIImagePickerControllerSourceTypeCamera;
             [picker setMediaTypes:[NSArray arrayWithObject:(NSString *)kUTTypeImage]];
             picker.delegate = self;
    
            popover = [[UIPopoverController alloc] initWithContentViewController:picker];
            [popover presentPopoverFromRect:CGRectMake(btn.frame.size.width,btn.frame.size.height/2,1,1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    
        }
        else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unsupported!"
                                                            message:@"Camera does not support photo capturing."
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
    
     }
     else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unavailable!"
                                                        message:@"This device does not have a camera."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
     }
    }
    
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)myimage editingInfo:(NSDictionary *)editingInfo
    {
        myimage = [myimage fixOrientation];
    
        [picker dismissModalViewControllerAnimated:YES];
        [popover dismissPopoverAnimated:YES];
    }
    

    我希望它可以帮助您更好地理解。

    【讨论】:

    • 它像往常一样崩溃。我在我的 iPad 上使用 iOS6,我不能使用dismissModalViewControllerAnimated。我只能使用 [self dismissViewControllerAnimated:YES completion:Nil];这也返回错误
    • 嗨 Vikr,请查看我更新的代码并使用 didFinishPickingImage: 而不是 didFinishPickingMediaWithInfo: 。如果您使用的是 ipad,则选择器将与 UIPopoverController 一起使用,因此如果任何 UIPopoverController 对象可用,则使用 [popover dismissPopoverAnimated:YES];为它。
    • 嗨 Vikr,'在 iPad 上,UIImagePickerController 必须通过 UIPopoverController 呈现'。这是苹果的规定,否则您的应用将在应用商店中被拒绝,因此对于 ipad,通过 UIPopoverController 实现 UIImagePickerController
    • 是的,我正在 iPad 上测试我的应用程序。实际上 ViewWillAppear 正在玩破坏运动。如果我评论它,上面的代码正在工作。但随后,viewwillappear 中调用的其他工作似乎无法正常工作。
    • 你应该在 getAlbum 中应用 UIPopoverController 来从相机中挑选图像。如果 ipad 应用程序中没有 UIPopoverController,Apple 会在审核期间拒绝您的应用程序,因此请尝试使用 UIPopoverController 应用它。我正在使用您的 getAlbum 方法更新我的代码。使用它并应用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多