我有一个通用的应用程序,允许从设备照片库中选择一个图像供以后操作,代码在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");
}
答案 0 :(得分:21)
而不是使用下面的代码。
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
试试此代码
[self dismissModalViewControllerAnimated:YES];
并检查您是否在界面文件中添加了UIImagePickerControllerDelegate
。
解决方案:(来自我的评论)
试试这个[self.imgPicker dismissModalViewControllerAnimated:YES];
这将有效。
对于iOS 7:要关闭当前视图控制器
[self.imgPicker dismissViewControllerAnimated: YES completion: NULL];
答案 1 :(得分:8)
在您的界面文件中添加UIImagePickerControllerDelegate
然后在.m
文件
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissModalViewControllerAnimated:YES];
}
我希望它能解决你的问题。
答案 2 :(得分:2)
检查调用选择器的父控制器的viewWillAppear或viewDidAppear方法。在iPhone上,这个方法将在选择器视图消失后调用。在popover消失在iPad上后,它们不会被调用。我刚刚在我的代码中发现错误,我在viewWillAppear中为所选图像设置nil为ivar。我花了两天时间来了解发生的事情;) 祝你好运!
答案 3 :(得分:2)
最简单的解决方案:
在界面文件中添加UIImagePickerControllerDelegate
然后在.m文件中实现此代码
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
答案 4 :(得分:1)
Swift 3.0
感谢MBH,这对我的Xcode 8和iOS 10项目起了作用:
internal func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
dismiss(animated: true, completion: nil)
}
答案 5 :(得分:0)
在Swift中关闭它:
将这些协议添加到ViewController:UINavigationControllerDelegate, UIImagePickerControllerDelegate
internal func imagePickerControllerDidCancel(picker: UIImagePickerController){
dismissViewControllerAnimated(true, completion: nil)
}