▼iPhoneプログラミングメモ▼
カメラとフォトアルバム

カメラとフォトアルバムを利用するプログラムを作成する。
 

ソースコードの記述
CameraEx.h
#import <UIKit/UIKit.h>

//CameraExの宣言
@interface CameraEx : UIViewController <
    UINavigationControllerDelegate,   
    UIImagePickerControllerDelegate> {
    UIImageView* _imageView;
}
@end

CameraEx.m
#import "CameraEx.h"

#define BTN_CAMERA 0
#define BTN_READ   1
#define BTN_WRITE  2

//CameraExの実装
@implementation CameraEx

//アラートの表示
- (void)showAlert:(NSString*)title text:(NSString*)text {
    UIAlertView* alert=[[[UIAlertView alloc] 
        initWithTitle:title message:text delegate:nil 
        cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [alert show];
}

//テキストボタンの生成
- (UIButton*)makeButton:(CGRect)rect text:(NSString*)text tag:(int)tag {
    UIButton* button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:text forState:UIControlStateNormal];
    [button setFrame:rect];
    [button setTag:tag];
    [button addTarget:self action:@selector(clickButton:) 
        forControlEvents:UIControlEventTouchUpInside];
    return button;
}

//イメージビューの生成
- (UIImageView*)makeImageView:(CGRect)rect image:(UIImage*)image {
    UIImageView* imageView=[[[UIImageView alloc] init] autorelease];
    [imageView setFrame:rect];
    [imageView setImage:image];
    [imageView setContentMode:UIViewContentModeScaleAspectFit];
    return imageView;
}

//イメージピッカーのオープン
- (void)openPicker:(UIImagePickerControllerSourceType)sourceType {
    //カメラとフォトアルバムの利用可能チェック
    if (![UIImagePickerController isSourceTypeAvailable:sourceType]) {
        [self showAlert:@"" text:@"利用できません"];
        return;
    }

    //イメージピッカー
    UIImagePickerController* picker=
        [[[UIImagePickerController alloc] init] autorelease]; 
    picker.sourceType=sourceType; 
    picker.delegate=self;
    
    //ビューコントローラのビューを開
    [self presentModalViewController:picker animated:YES]; 
}

//イメージピッカーのイメージ取得時に呼ばれる
- (void)imagePickerController:(UIImagePickerController*)picker 
    didFinishPickingMediaWithInfo:(NSDictionary*)info {
    //イメージの指定
    UIImage* image=[info objectForKey:UIImagePickerControllerOriginalImage];
    [_imageView setImage:image];
        
    //ビューコントローラのビューを閉じる
    [[picker parentViewController] 
        dismissModalViewControllerAnimated:YES];
}

//イメージピッカーのキャンセル時に呼ばれる
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)pickerCtl {
    //ビューコントローラのビューを閉じる
    [[pickerCtl parentViewController] 
        dismissModalViewControllerAnimated:YES];
}

//初期化
- (void)viewDidLoad {
    [super viewDidLoad];

    //カメラボタンの生成
    UIButton* btnCamera=[self makeButton:CGRectMake(0,0,200,40) 
        text:@"カメラ" tag:BTN_CAMERA];
    [self.view addSubview:btnCamera];
        
    //フォト読み込みボタンの生成
    UIButton* btnRead=[self makeButton:CGRectMake(0,50,200,40) 
        text:@"フォト読み込み" tag:BTN_READ];
    [self.view addSubview:btnRead];

    //フォト書き込みボタンの生成
    UIButton* btnWrite=[self makeButton:CGRectMake(0,100,200,40) 
        text:@"フォト書き込み" tag:BTN_WRITE];
    [self.view addSubview:btnWrite];
    
    //イメージビューの生成
    _imageView=[[self makeImageView:CGRectMake(0,170,200,200) 
        image:nil] retain];
    [self.view addSubview:_imageView];
}

//メモリ解放
- (void)dealloc {
    [_imageView release];
    [super dealloc];
}

//ボタンクリック時のイベント処理
- (IBAction)clickButton:(UIButton*)sender {
    if (sender.tag==BTN_CAMERA) {
        [self openPicker:UIImagePickerControllerSourceTypeCamera];
    } else if (sender.tag==BTN_READ) {
        [self openPicker:UIImagePickerControllerSourceTypePhotoLibrary];
    } else if (sender.tag==BTN_WRITE) {
        UIImage* image=[_imageView image];
        if (image==nil) return;
        
        //イメージのフォトアルバムへの書き込み
        UIImageWriteToSavedPhotosAlbum(image,self,
            @selector(finishExport:didFinishSavingWithError:contextInfo:),NULL);
    }
}

//フォト書き込み完了
- (void)finishExport:(UIImage*)image
    didFinishSavingWithError:(NSError*)error
    contextInfo:(void*)contextInfo {
    if (error==nil) {
        [self showAlert:@"" text:@"フォト書き込み完了"];
    } else {
        [self showAlert:@"" text:@"フォト書き込み失敗"];
    }
}

//画面を端末の向きにあわせて回転
- (BOOL)shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)orientation {
    return YES;
}
@end


−戻る−