我正在尝试使用 UIImagePickerController 从用户的 iPhone/iPad 上的照片中抓取照片。这段代码在 iPhone 上工作得很好,但是当我在 iPad 上运行它时,调试器给我消息“由于未捕获的异常‘NSInvalidArgumentException’而终止应用程序,原因:‘在 iPad 上,UIImagePickerController 必须通过 UIPopoverController 呈现’” 强>。我是 Objective-C 的新手,所以我不确定如何编辑此代码以在 iPad 上运行时使用 UIPopoverController。我不想创建 2 个新的 View Controller ,所以我想知道是否有人知道我需要添加什么代码才能让它在 iPhone 和 iPad 上工作。这是我的 View Controller 中的代码:
ViewController.h:
@interface PhotoViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
UIButton *grabButton;
UIImageView *image;
UIImagePickerController *imgPicker;
}
@property (strong, nonatomic) IBOutlet UIButton *grabButton;
@property (strong, nonatomic) IBOutlet UIImageView *image;
@property (strong, nonatomic) UIImagePickerController *imgPicker;
- (IBAction)grabImage;
@end
ViewController.m:
#import "PhotoViewController.h"
@implementation PhotoViewController
@synthesize grabButton;
@synthesize image;
@synthesize imgPicker;
- (IBAction)grabImage {
[self presentModalViewController:self.imgPicker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
image.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"Photo Gallery", @"Photo Gallery");
self.tabBarItem.image = [UIImage imageNamed:@"42-photos.png"];
}
return self;
}
- (void)viewDidLoad
{
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsImageEditing = YES;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[super viewDidLoad];
}
提前致谢!
最佳答案
您可以通过该错误消息找到答案!
将 UIPopOverController 与适用于 iPad 的 UIImagePicker 一起使用。
想知道UIPopOverController怎么用,可以看看这个tutorial !
另一个 youtube 教程 - http://www.youtube.com/watch?v=6Gc3kxVwfmE
关于objective-c - iOS:UIImagePickerController 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9863666/