1

只是为了确保我的所有代码都正确,这是我的头文件:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIImagePickerControllerDelegate,UINavigationControllerDelegate> {

    IBOutlet UIImageView *imageView;
    IBOutlet UIButton *choosePhoto;
    IBOutlet UIButton *takePhoto;

}

@property(nonatomic, retain) UIButton *choosePhoto;

@property(nonatomic, retain) UIButton *takePhoto;

@property(nonatomic, retain) UIImageView *imageView;

-(IBAction)getPhoto:(id)sender;
-(IBAction)takePhoto:(id)sender;

@end

然后这是我的实现:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize imageView,choosePhoto,takePhoto;

- (void)viewDidLoad
{
    [super viewDidLoad];

}

-(IBAction)getPhoto:(id)sender {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [self presentViewController:picker animated:YES completion:nil];
}

-(IBAction)takePhoto:(id)sender {

    UIImagePickerController *picker2 = [[UIImagePickerController alloc] init];
    picker2.delegate = self;
    picker2.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:picker2 animated:YES completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [picker dismissViewControllerAnimated:YES completion:nil];
    imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}

-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}

@end

我的界面生成器中有两个UIButtons和一个UIIImageView。测试应用程序构建良好,完全没有问题,但是当我按下选择照片或拍照按钮时,应用程序崩溃。这仅仅是因为模拟器在技术上没有相机或照片库可供选择吗?这是它给我的错误: UIStatusBarStyleBlackTranslucent在此设备上不可用。

Cameratest[8290:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController

非常感谢您的建议!

4

2 回答 2

8

来自苹果:

http://developer.apple.com/library/ios/#documentation/Xcode/Conceptual/ios_development_workflow/25-Using_iOS_Simulator/ios_simulator_application.html

硬件模拟支持

iOS 模拟器不模拟加速度计或相机硬件。

此外,H2CO3 是正确的,您遇到了不同的问题,并且错误消息非常清楚:

'在 iPad 上,UIIMagePickerController 必须通过 UIPopoverController 呈现'

快速的谷歌搜索将我指向下面的链接。查看祖拜尔的回应。我认为您需要在 iPad 上运行时在弹出窗口中显示 UIImagePickerController。

UIStatusBarStyleBlackTranslucent 在此设备上不可用

于 2013-03-28T22:18:41.230 回答
0

没有模拟器不支持模拟相机。对于相机,您必须在设备上运行应用程序

于 2013-10-28T07:38:07.213 回答