9

我有一个通用应用程序,允许从设备照片库中选择图像以供以后操作,代码在 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");
}
4

6 回答 6

21

而不是使用下面的代码。

[[picker parentViewController] dismissModalViewControllerAnimated:YES];

试试这个代码

[self dismissModalViewControllerAnimated:YES];

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

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

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

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

[self.imgPicker dismissViewControllerAnimated: YES completion: NULL];
于 2012-04-21T17:26:58.997 回答
8

添加UIImagePickerControllerDelegate你的接口文件

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

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

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

于 2012-04-22T06:43:13.020 回答
2

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

于 2013-12-02T23:09:21.503 回答
2

一个最简单的解决方案:

在你的界面文件中添加 UIImagePickerControllerDelegate

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

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker  dismissViewControllerAnimated:YES completion:nil];
}
于 2015-06-26T12:53:50.760 回答
1

斯威夫特 3.0

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

internal func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
    dismiss(animated: true, completion: nil)
}
于 2017-01-10T00:58:20.013 回答
0

在 Swift 中关闭它:

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

internal func imagePickerControllerDidCancel(picker: UIImagePickerController){
    dismissViewControllerAnimated(true, completion: nil)
}
于 2015-12-22T21:52:37.787 回答