objective-c - iOS:UIImagePickerController 问题

标签 objective-c ios xcode uiimagepickercontroller

我正在尝试使用 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/

相关文章:

objective-c - 使用未声明的标识符错误

objective-c - 覆盖父类(super class)中方法的 Objective C 子类

iphone - 使用 Core Motion 计算倾斜角

objective-c - 如何在 iPhone 中使带有图像背景的按钮变圆?

xcode - 带圆角的 NSVisualEffectView

iphone - 如何在我的iPhone项目中实现Singleton Pattern?

objective-c - NSArrayController/NSTreeController 和 Cocoa View

ios - swift ,iOS 8+ 框架

iphone - iOS OpenEars无法识别准确的声音

iphone - 为特定设备更新 Xcode 8 中的 UI 约束