Javaな日々

NO JAVA, NO LIFE.

カメラ, カメラロール, フォトアルバムから画像を選択してメール添付する

f:id:yksris:20121115183518p:image:h320

アクションシートを利用して画像をどこから(カメラ,カメラロール,フォトアルバム)選択するかというのと,選択した画像をメールに添付して送信するというのを実装してみます.

環境

手順

Interface BuilderやStoryboadにて適当にボタンを2つ作成,それぞれのIBActionをビューコントローラーのクラスのヘッダーファイルに割り当てておく.
今回はStoryboadにてボタンを2つ作成し,PhotoSelectViewControllerというUIViewControllerクラスを継承したクラスのヘッダーファイルにAction(pressedCameraButton, pressedComposeButton)を割り当てた.

PhotoSelectViewController.h
#import <UIKit/UIKit.h>

@interface PhotoSelectViewController : UIViewController <UIActionSheetDelegate>

@property (strong, nonatomic) UIImage *image;

- (IBAction)pressedCameraButton:(id)sender;
- (IBAction)pressedComposeButton:(id)sender;

@end
PhotoSelectViewController.m
#import "PhotoSelectViewController.h"
#import <MessageUI/MessageUI.h>

@interface PhotoSelectViewController ()

@end

@implementation PhotoSelectViewController

// 省略

- (IBAction)pressedCameraButton:(id)sender {
    
    UIActionSheet *sheet = [[UIActionSheet alloc]
             initWithTitle:@"送信する写真を選択"
             delegate:self
             cancelButtonTitle:@"キャンセル"
             destructiveButtonTitle:nil
             otherButtonTitles:@"フォトライブラリー", @"カメラ", @"カメラロール", nil];
    [sheet showInView:self.view];
}

- (IBAction)pressedComposeButton:(id)sender {
    
    MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
    // メールの送信・キャンセルをデリゲートで受け取るためにセット
    [mailComposeViewController setMailComposeDelegate:self];
    [mailComposeViewController setToRecipients:[NSArray arrayWithObject:@"example@example.com"]];
    [mailComposeViewController setMessageBody:@"メールの本文" isHTML:NO];
    
    // _imageのnilチェック
    if (_image){
        // 圧縮率
        CGFloat compressionQuality = 0.8;
        // UIImageJPEGRepresentationでJPEG圧縮
        NSData *attachData = UIImageJPEGRepresentation(_image, compressionQuality);
        // 圧縮した画像を添付
        [mailComposeViewController addAttachmentData:attachData
                                            mimeType:@"image/jpeg"
                                            fileName:@"image.jpg"];
    }
    
	[self presentModalViewController:mailComposeViewController animated:YES];
}

// アクションシートのデリゲートメソッド(アクションシートのボタンが押された時の処理)
- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    UIImagePickerControllerSourceType sourceType;
    UIImagePickerController *ipc;
    
    switch (buttonIndex) {
        case 0:
            // フォトライブラリーを表示
            sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            if (![UIImagePickerController isSourceTypeAvailable:sourceType]) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"エラー" message:@"フォトライブラリーを表示できません" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
                return;
            }
            
            ipc = [[UIImagePickerController alloc] init];
            [ipc setSourceType:sourceType];
            [ipc setDelegate:self];
            [self presentModalViewController:ipc animated:YES];
            break;
            
        case 1:
            // カメラを起動
            sourceType = UIImagePickerControllerSourceTypeCamera;
            if (![UIImagePickerController isSourceTypeAvailable:sourceType]) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"エラー" message:@"カメラを起動できません" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
                return;
            }
            
            ipc = [[UIImagePickerController alloc] init];
            [ipc setSourceType:sourceType];
            [ipc setDelegate:self];
            [self presentModalViewController:ipc animated:YES];
            break;
            
        case 2:
            // カメラロールを表示
            sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            if (![UIImagePickerController isSourceTypeAvailable:sourceType]) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"エラー" message:@"カメラロールを表示できません" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
                return;
            }
            
            ipc = [[UIImagePickerController alloc] init];
            [ipc setSourceType:sourceType];
            [ipc setDelegate:self];
            [self presentModalViewController:ipc animated:YES];
            break;
            
        default:
            break;
    }
}

// アクションシートのデリゲートメソッド(画像を選択したあとの処理)
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    _image = image;
    
    if ([picker respondsToSelector:@selector(presentingViewController)]) {
        [[picker presentingViewController] dismissModalViewControllerAnimated:YES];
    } else {
        [[picker parentViewController] dismissModalViewControllerAnimated:YES];
    }
}

@end