[英]Problems with UIImagePickerController cancel button not working
我有一個通用的應用程序,允許從設備照片庫中選擇一個圖像供以后操作,代碼在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");
}
而不是使用下面的代碼。
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
試試這個代碼
[self dismissModalViewControllerAnimated:YES];
還檢查你的接口文件中是否添加了UIImagePickerControllerDelegate
。
解決方案:(來自我的評論)
試試這個[self.imgPicker dismissModalViewControllerAnimated:YES];
這會奏效。
對於iOS 7:要關閉當前視圖控制器
[self.imgPicker dismissViewControllerAnimated: YES completion: NULL];
在接口文件中添加UIImagePickerControllerDelegate
然后在.m
文件中實現此代碼
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissModalViewControllerAnimated:YES];
}
我希望它能解決你的問題。
檢查調用選擇器的父控制器的viewWillAppear或viewDidAppear方法。 在iPhone上,這個方法將在選擇器視圖消失后調用。 在popover消失在iPad上后,它們不會被調用。 我剛剛在我的代碼中發現錯誤,我在viewWillAppear中為所選圖像設置nil為ivar。 我花了兩天時間來了解發生了什么;)祝你好運!
最簡單的解決方案:
在接口文件中添加UIImagePickerControllerDelegate
然后在.m文件中實現此代碼
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
Swift 3.0
感謝MBH,我在Xcode 8和iOS 10項目中為我工作:
internal func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
dismiss(animated: true, completion: nil)
}
在Swift中關閉它:
將這些協議添加到ViewController后: UINavigationControllerDelegate, UIImagePickerControllerDelegate
internal func imagePickerControllerDidCancel(picker: UIImagePickerController){
dismissViewControllerAnimated(true, completion: nil)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.