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

时间:2022-08-19 00:04:11

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");
}

6 个解决方案

#1


20  

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]; This will work.

试试这个[self.imgPicker dismissModalViewControllerAnimated:YES];这会奏效。

For iOS 7: To dismiss a present view controller

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

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

#2


8  

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.

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

#3


2  

check viewWillAppear or viewDidAppear methods of your parent controller which calls the picker. On iPhone this methods will be called after picker view disappeared. They will not be called after popover disappeared on iPad. I just found error in my code where i set nil to ivar for picked image in viewWillAppear. it take me two days to understand what is happened ;) Good luck!

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

#4


1  

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

#5


1  

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)
}

#6


0  

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)
}

#1


20  

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]; This will work.

试试这个[self.imgPicker dismissModalViewControllerAnimated:YES];这会奏效。

For iOS 7: To dismiss a present view controller

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

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

#2


8  

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.

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

#3


2  

check viewWillAppear or viewDidAppear methods of your parent controller which calls the picker. On iPhone this methods will be called after picker view disappeared. They will not be called after popover disappeared on iPad. I just found error in my code where i set nil to ivar for picked image in viewWillAppear. it take me two days to understand what is happened ;) Good luck!

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

#4


1  

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

#5


1  

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)
}

#6


0  

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)
}