[iPhone] イメージピッカーの使い方
iPhone/iPod touch 内の写真をアプリから選択するには UIImagePickerController を使用します。カメラを起動して写真を撮ったりするのもこのイメージピッカーを使用します。
UINavigationController と UIImagePickerController の Delegate を使用するのでヘッダファイルに追加。
ViewController.h
@interface ViewController : UIViewController
<UINavigationControllerDelegate,
UIImagePickerControllerDelegate> {
}
実際の処理部分。sourceType に UIImagePickerControllerSourceTypePhotoLibrary を設定すると写真ライブラリが開きます。
ViewController.m
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentModalViewController:picker animated:YES]; [picker release];
ライブラリで写真を選択すると imagePickerContorller:didFinishPickingImage:editingInfo: に選択した写真が UIImage *image として渡されます。
- (void) imagePickerController:(UIImagePickerController*)picker
didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary*)editingInfo {
// image を処理
[picker dismissModalViewControllerAnimated:YES];
}
写真選択をキャンセルしたときは imagePickerControllerDidCancel: が呼ばれます。
- (void) imagePickerControllerDidCancel:(UIImagePickerController*)picker {
//キャンセル時の処理
}
注意点は UIImagePickerController は内部で UINavigationContorller を使用しているので、UINavigationContoller を使用している時に UIImagePickerController を呼ぶとナビゲーションバーが二重に表示されてしまいます。タブバーや単独のビューから呼ぶのがよいでしょう。
関連する投稿
One comment
1cassette への返信 コメントをキャンセル
Additional comments powered by BackType
1burmese…
…