UIImagePicker を使ってアプリに画像を取り込む方法のメモです。

(iOS5 で試しています。)

カメラで撮った写真、保存してある画像を使った

アプリケーションを作りたいときは、

UIImagePicker を使うと、便利です。

画面をタッチすると、ImagePicker が表示される

サンプルです。

#import “ViewController.h”

@interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {

    UIImagePickerController *ipc;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    ipc = [[UIImagePickerController alloc] init];

    ipc.delegate = self;

    

    // Camera が使えるか? 

    // シミュレータ、や古いカメラのついていないiPhone 

    // カメラではなく、画像フォルダを開くように

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

        ipc.sourceType = UIImagePickerControllerSourceTypeCamera;

    }

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [self presentModalViewController:ipc animated:YES];

}

@end