使用的UIImagePickerController问题取消按钮不工作
我有一个通用的应用程序,允许选择从设备的照片库中供以后处理的图像,代码工作正常在iPad上,但什么也没有发生在iPhone,甚至没有取消按钮,选择图像后没有任何事情发生,这里是我的代码:使用的UIImagePickerController问题取消按钮不工作
-(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");
}
而不是使用下面的代码。
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
试试这个代码
[self dismissModalViewControllerAnimated:YES];
,并检查你在接口文件中添加UIImagePickerControllerDelegate
。
解决方案:(从我的意见)
试试这个[self.imgPicker dismissModalViewControllerAnimated:YES];
这将工作。
对于iOS 7:要关闭当前视图控制器
[self.imgPicker dismissViewControllerAnimated: YES completion: NULL];
使用self使所有视图消失所有我需要的是取消图像选择器视图,它工作正常iPad,但它不适用于iPhone – 2012-04-21 20:51:30
然后试试这个[self.imgPicker dismissModalViewControllerAnimated:YES]; – 2012-04-22 05:04:29
对于iOS 7,这似乎工作:[picker dismissViewControllerAnimated:YES completion:NULL]; – russes 2014-01-01 03:18:22
在接口文件
添加UIImagePickerControllerDelegate
,然后执行该代码在你.m
文件
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissModalViewControllerAnimated:YES];
}
我希望它能解决你的问题。
检查viewWillAppear中或调用选择器的父控制器的viewDidAppear方法。在iPhone上,这个方法将在拾取器视图消失后被调用。 Popover在iPad上消失后,它们将不会被调用。我只是在我的代码中发现错误,我在viewWillAppear中将所选图像设置为ivar。需要两天的时间才能了解发生的情况;) 祝你好运!
一个简单的解决方案:
添加UIImagePickerControllerDelegate在接口文件
,然后在.m文件
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
实现该代码对于斯威夫特关闭它:
将这些协议添加到您之后ViewController:UINavigationControllerDelegate, UIImagePickerControllerDelegate
internal func imagePickerControllerDidCancel(picker: UIImagePickerController){
dismissViewControllerAnimated(true, completion: nil)
}
请解释它为什么可行。 – 2015-12-23 00:58:21
@RohitGupta我测试了它,它工作正常。因为我在ViewController上实现了委托'UIImagePickerControllerDelegate' – MBH 2015-12-23 06:53:47
雨燕3.0
由于MBH这在我的Xcode 8和iOS的10项目工作对我来说:
internal func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
dismiss(animated: true, completion: nil)
}
检查你是否在你的界面文件中添加uiimagepickercontrollerdelegate? – 2012-04-21 17:21:57
虽然这不能解决您的问题,但您应该切换到使用'presentViewController:animated:completion:'和'dismissViewControllerAnimated:completion:'来代替'presentModalViewController:animated:''和'dismissModalViewControllerAnimated:',因为后两个已弃用(请参阅[docs](http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html))。 – 2012-04-22 09:28:06
使用[self dismissModalViewControllerAnimated:YES]读取所有建议后;确实解决了一些问题,但现在发生的情况是,具有打开图像选择器的按钮的视图消失了,有什么想法? – 2012-04-22 13:56:03