ボタンを押すとアクションシートを表示してアルバムかカメラを選択、
撮影や選択した画像をイメージビューに表示します。
今回は、
UIActionSheetや
UIImagePickerControllerの
isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary、
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCameraを使います。
xibファイルに、Buttonとイメージビューを作成してください。
ButtonのSent Eventsは、Touch Up Insideに"as"を接続してください。
ImageViewのReferencing Outletsは、aImageViewと接続してください。
ViewController.h
==================================
@interface ViewController : UIViewController
<
UIActionSheetDelegate,
UIImagePickerControllerDelegate
>
{
UIImageView *aImageView;
}
@property (nonatomic, retain) IBOutlet UIImageView *aImageView;
-(IBAction)as:(id)sender;
@end
==================================
ViewController.m
==================================
@implementation ViewController
@synthesize aImageView;
-(IBAction)as:(id)sender{
UIActionSheet *as = [[UIActionSheet alloc] init];
as.delegate = self;
as.title = @"選択してください。";
[as addButtonWithTitle:@"アルバム"];
[as addButtonWithTitle:@"カメラ"];
[as addButtonWithTitle:@"キャンセル"];
as.cancelButtonIndex = 2;
// as.destructiveButtonIndex = 0; //ボタンを赤くする
[as showInView:self.view];
}
// 画像を取得する
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSLog(@"%@", [image description]);
[aImageView setImage:image];
[self dismissModalViewControllerAnimated:YES];
}
//画像の保存完了時に呼ばれるメソッド
-(void)targetImage:(UIImage*)image
didFinishSavingWithError:(NSError*)error contextInfo:(void*)context{
if(error){
// 保存失敗時
}else{
// 保存成功時
}
}
//画像の選択がキャンセルされた時に呼ばれる
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissModalViewControllerAnimated:YES]; // モーダルビューを閉じる
// 何かの処理
}
-(void)doAlbum {
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
[picker setDelegate:self];
[self presentModalViewController:picker animated:YES];
[picker release];
}
-(void)doCamera {
if([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
[ipc setSourceType:UIImagePickerControllerSourceTypeCamera];
[ipc setDelegate:self];
[ipc setAllowsEditing:NO];
[self presentModalViewController:ipc animated:YES];
[ipc release];
}
else {
//カメラが使えない場合
}
}
-(void)actionSheet:(UIActionSheet*)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0: //アルバム
// 1番目のボタンが押されたときの処理を記述する
NSLog(@"アルバム");
[self doAlbum];
break;
case 1: //カメラ
// 2番目のボタンが押されたときの処理を記述する
NSLog(@"カメラ");
[self doCamera];
break;
case 2: //キャンセル
// 3番目のボタンが押されたときの処理を記述する
NSLog(@"キャンセル");
break;
}
}
※ これ以下は変更してません ※
==================================
これで、取得した画像をイメージビューに表示できるはずです。
細かい画像サイズ等の指定はしていないので、
ビューのサイズによっては縦横比が不細工になるかもしれません。
その辺の細かいところはまだ勉強中です。
明日以降何を更新するかがまだ決まっていません。
もうネタ切れです(笑)