简体   繁体   English

UIImagePickerController取消按钮无法正常工作的问题

[英]Problems with UIImagePickerController cancel button not working

I have an universal app that allows to select an image from the device photo library for later manipulation, the code works fine on the iPad but nothing happens on the iPhone, not even the cancel button and after an image is selected nothing happens neither here is my code: 我有一个通用的应用程序,允许从设备照片库中选择一个图像供以后操作,代码在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");
}

Instead of using below code. 而不是使用下面的代码。

[[picker parentViewController] dismissModalViewControllerAnimated:YES];

Try this code 试试这个代码

[self dismissModalViewControllerAnimated:YES];

and also check did you add UIImagePickerControllerDelegate in your interface file. 还检查你的接口文件中是否添加了UIImagePickerControllerDelegate

SOLUTION: (From my comment) 解决方案:(来自我的评论)

Try this [self.imgPicker dismissModalViewControllerAnimated:YES]; 试试这个[self.imgPicker dismissModalViewControllerAnimated:YES]; This will work. 这会奏效。

For iOS 7: To dismiss a present view controller 对于iOS 7:要关闭当前视图控制器

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

Add UIImagePickerControllerDelegate in your interface file 在接口文件中添加UIImagePickerControllerDelegate

and then implement this code in your .m file 然后在.m文件中实现此代码

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

i hope it's solve your problem. 我希望它能解决你的问题。

check viewWillAppear or viewDidAppear methods of your parent controller which calls the picker. 检查调用选择器的父控制器的viewWillAppear或viewDidAppear方法。 On iPhone this methods will be called after picker view disappeared. 在iPhone上,这个方法将在选择器视图消失后调用。 They will not be called after popover disappeared on iPad. 在popover消失在iPad上后,它们不会被调用。 I just found error in my code where i set nil to ivar for picked image in viewWillAppear. 我刚刚在我的代码中发现错误,我在viewWillAppear中为所选图像设置nil为ivar。 it take me two days to understand what is happened ;) Good luck! 我花了两天时间来了解发生了什么;)祝你好运!

An easiest solution: 最简单的解决方案:

Add UIImagePickerControllerDelegate in your interface file 在接口文件中添加UIImagePickerControllerDelegate

and then implement this code in your .m file 然后在.m文件中实现此代码

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

Swift 3.0 Swift 3.0

Thanks to MBH this worked for me in my Xcode 8 and iOS 10 project: 感谢MBH,我在Xcode 8和iOS 10项目中为我工作:

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

For closing it in Swift: 在Swift中关闭它:

After adding those protocols to you ViewController: UINavigationControllerDelegate, UIImagePickerControllerDelegate 将这些协议添加到ViewController后: UINavigationControllerDelegate, UIImagePickerControllerDelegate

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM