すべてのコードが正しいことを確認するために、ヘッダー ファイルを次に示します。
#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
私のインターフェースビルダーには2つと1つがUIButtons
あります。UIIImageView
テスト アプリは正常にビルドされ、まったく問題はありませんが、写真を選択するボタンまたは写真を撮るボタンを押すと、アプリがクラッシュします。これは、シミュレーターが技術的にカメラやライブラリから選択できる写真を持っていないからですか? これは私に与えるエラーです: UIStatusBarStyleBlackTranslucent
このデバイスでは利用できません。
Cameratest[8290:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController
アドバイスをいただければ幸いです。