【问题标题】:Problems with UIImagePickerController cancel button not workingUIImagePickerController 取消按钮不起作用的问题
【发布时间】:2012-04-21 17:00:04
【问题描述】:

我有一个通用应用程序,允许从设备照片库中选择图像以供以后操作,代码在 iPad 上运行良好,但在 iPhone 上没有任何反应,甚至没有取消按钮,并且在选择图像后没有任何反应这也不是我的代码:

-(IBAction)grabImage:(id)sender
{
    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
    imgPicker = [[UIImagePickerController alloc] init];
    [imgPicker setDelegate:self];

    popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
    [popover setDelegate:self];

    CGPoint position        = [view1.superview convertPoint:view1.frame.origin toView:nil];
    CGRect popOverFrame     = CGRectMake( position.x, position.y, self.view.frame.size.width, self.view.frame.size.height );

    [popover presentPopoverFromRect:popOverFrame inView:self.view permittedArrowDirections:nil animated:NO];
    [popover setPopoverContentSize:CGSizeMake(320, 480)];
    [imgPicker release];
}
else
{
    imgPicker = [[UIImagePickerController alloc] init];
    imgPicker.delegate = self;
    imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:self.imgPicker animated:YES];
    [imgPicker release];
}
}


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];

CGImageRef imgRef = pickedImage.CGImage;

    app->setImage( pickedImage, CGImageGetWidth(imgRef), CGImageGetHeight(imgRef) );

    [[picker parentViewController] dismissModalViewControllerAnimated:YES];

// Enable texture siwth after an image has been loaded
[textureSwitch setEnabled:YES];
[textureSwitch setOn:YES];
app->isTextureDrawingOn     = [textureSwitch isOn];

[fillsSwitch setOn:NO];
app->isFillsDrawingOn       = [fillsSwitch isOn];

    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
    [popover dismissPopoverAnimated:YES];
}
ofLog(OF_LOG_VERBOSE, "cancel after selection");
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];

if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
        [popover dismissPopoverAnimated:YES];
    }

ofLog(OF_LOG_VERBOSE, "did cancel");
}

【问题讨论】:

  • 检查是否在接口文件中添加了uiimagepickercontrollerdelegate??
  • 虽然这不能解决您的问题,但您应该改用presentViewController:animated:completion:dismissViewControllerAnimated:completion: 代替presentModalViewController:animated:dismissModalViewControllerAnimated:,因为后两者已被弃用(请参阅@987654321 @)。
  • 在阅读所有使用 [self dismissModalViewControllerAnimated:YES] 的建议后;确实在一定程度上解决了这个问题,但现在发生的事情是具有打开图像选择器的按钮的视图消失了,有什么想法吗?
  • 解决我的 vis 被推到顶部的问题,我所要做的就是在取消时重新处理所有内容

标签: objective-c ios uiimagepickercontroller


【解决方案1】:

而不是使用下面的代码。

[[picker parentViewController] dismissModalViewControllerAnimated:YES];

试试这个代码

[self dismissModalViewControllerAnimated:YES];

并检查您是否在接口文件中添加了UIImagePickerControllerDelegate

解决方案:(来自我的评论)

试试这个[self.imgPicker dismissModalViewControllerAnimated:YES]; 这将起作用。

对于 iOS 7: 关闭当前视图控制器

[self.imgPicker dismissViewControllerAnimated: YES completion: NULL];

【讨论】:

  • 使用 self 使所有视图消失我所需要的只是取消图像选择器视图,它在 iPad 上可以正常工作,但在 iPhone 上却不行
  • 然后试试这个 [self.imgPicker dismissModalViewControllerAnimated:YES];
  • 对于 iOS 7,这似乎有效:[picker dismissViewControllerAnimated: YES completion: NULL];
【解决方案2】:

在你的接口文件中添加UIImagePickerControllerDelegate

然后在您的 .m 文件中实现此代码

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissModalViewControllerAnimated:YES];
}

我希望它能解决你的问题。

【讨论】:

    【解决方案3】:

    检查调用选择器的父控制器的 viewWillAppear 或 viewDidAppear 方法。在 iPhone 上,选择器视图消失后将调用此方法。在 iPad 上 popover 消失后它们将不会被调用。我刚刚在我的代码中发现错误,我将 nil 设置为 ivar 以在 viewWillAppear 中选择图像。我花了两天时间才明​​白发生了什么;) 祝你好运!

    【讨论】:

      【解决方案4】:

      最简单的解决方案:

      在你的界面文件中添加 UIImagePickerControllerDelegate

      然后在您的 .m 文件中实现此代码

      - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
          [picker  dismissViewControllerAnimated:YES completion:nil];
      }
      

      【讨论】:

        【解决方案5】:

        Swift 3.0

        感谢 MBH,这在我的 Xcode 8 和 iOS 10 项目中为我工作:

        internal func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
            dismiss(animated: true, completion: nil)
        }
        

        【讨论】:

          【解决方案6】:

          在 Swift 中关闭它:

          将这些协议添加到您的 ViewController 后:UINavigationControllerDelegate, UIImagePickerControllerDelegate

          internal func imagePickerControllerDidCancel(picker: UIImagePickerController){
              dismissViewControllerAnimated(true, completion: nil)
          }
          

          【讨论】:

          • @RohitGupta 我测试了它,它工作正常。因为我在我的 ViewController 上实现了委托 UIImagePickerControllerDelegate
          猜你喜欢
          • 2019-08-16
          • 2019-06-23
          • 2015-04-10
          • 1970-01-01
          • 2014-02-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多