写真に何か書き込んでそれをフォトライブラリに保存するアプリ

(XcodeのiOS6 Simulatorで試しています。)

ポイント

・テキストが目立つようにUILabelにアウトラインを付ける

・UILabelとUIImageViewをUIImageとして保存する

サンプルアプリのコード

(※全部 ViewController.mに書いてみた。)

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

//

// UILabel with outline.

//

@interface OutLineLabel : UILabel

@end

@implementation OutLineLabel

– (void)drawTextInRect:(CGRect)rect

{

    CGSize shadowOffset = self.shadowOffset;

    UIColor *textColor = self.textColor;

    

    CGContextRef c = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(c, 4);

    CGContextSetLineJoin(c, kCGLineJoinRound);

    

    CGContextSetTextDrawingMode(c, kCGTextStroke);

    self.textColor = [UIColor whiteColor];

    [super drawTextInRect:rect];

    

    CGContextSetTextDrawingMode(c, kCGTextFill);

    self.textColor = textColor;

    self.shadowOffset = CGSizeMake(0, 0);

    [super drawTextInRect:rect];

    

    self.shadowOffset = shadowOffset;

}

@end

//

// ViewController

//

@interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextFieldDelegate>

@property (nonatomic, strong) UIImageView *pictureView;

@property (nonatomic, strong) UILabel *targetLabel;

@end

@implementation ViewController

@synthesize pictureView, targetLabel;

– (void)viewDidLoad

{

    [super viewDidLoad];

}

– (void)viewDidAppear:(BOOL)animated

{

    self.view.backgroundColor = [UIColor lightGrayColor];

    

    // イメージ表示用のViewを生成

    self.pictureView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, 320, 380)];

    [self.view addSubview:self.pictureView];

    

    // コントロールパネルの生成

    [self createControlPanel];

    

    // アウトライン付きのラベルを生成

    self.targetLabel = [[OutLineLabel alloc] init];

    self.targetLabel.backgroundColor = [UIColor clearColor];

    self.targetLabel.font = [UIFont boldSystemFontOfSize:40];

    self.targetLabel.center = self.view.center;

    [self.view addSubview:self.targetLabel];

    

    // Pan Gestureで移動できるようにする。

    self.targetLabel.userInteractionEnabled = YES;

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveText:)];

    [self.targetLabel addGestureRecognizer:pan];

}

– (void)createControlPanel

{

    UIView *panel = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];

    panel.backgroundColor = [UIColor darkGrayColor];

    [self.view addSubview:panel];

    

    // 入力フィールド

    UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 200, 30)];

    field.borderStyle = UITextBorderStyleRoundedRect;

    field.delegate = self;

    [panel addSubview:field];

    

    // カメラボタン

    UIView *photo = [[UIView alloc] initWithFrame:CGRectMake(270, 20, 40, 30)];

    photo.layer.cornerRadius = 5.0;

    photo.backgroundColor = [UIColor lightGrayColor];

    [panel addSubview:photo];

    

    // CALayerでアイコンを作る

    CALayer *body = [CALayer layer];

    body.frame = CGRectMake(2, 5, 36, 23);

    body.backgroundColor = [UIColor darkGrayColor].CGColor;

    body.cornerRadius = 5.0;

    [photo.layer addSublayer:body];

    CALayer *shutter = [CALayer layer];

    shutter.frame = CGRectMake(28, 2, 10, 10);

    shutter.backgroundColor = [UIColor darkGrayColor].CGColor;

    shutter.cornerRadius = 2.0;

    [photo.layer addSublayer:shutter];

    CALayer *lens = [CALayer layer];

    lens.frame = CGRectMake(0, 0, 16, 16);

    lens.backgroundColor = [UIColor darkGrayColor].CGColor;

    lens.borderColor = [UIColor lightGrayColor].CGColor;

    lens.borderWidth = 3.0;

    lens.cornerRadius = 8.0;

    lens.position = CGPointMake(20, 18);

    [photo.layer addSublayer:lens];

    

    // カメラボタンのタップでイメージピッカーを開く

    UITapGestureRecognizer *tapicon = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openImagePicker)];

    [photo addGestureRecognizer:tapicon];

    

    // save ボタン

    UILabel *saveBtn = [[UILabel alloc] init];

    saveBtn.text = @”save”;

    saveBtn.textColor = [UIColor whiteColor];

    saveBtn.center = CGPointMake(272, 70);

    saveBtn.backgroundColor = [UIColor clearColor];

    [saveBtn sizeToFit];

    [panel addSubview:saveBtn];

    // tapで写真を保存

    saveBtn.userInteractionEnabled = YES;

    UITapGestureRecognizer *tapSave = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(saveImage)];

    [saveBtn addGestureRecognizer:tapSave];

}

– (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

    // テキストを編集

    self.targetLabel.text = textField.text;

    [self.targetLabel sizeToFit];

    return YES;

}

– (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    // キーボードのReturnで編集を終了

    self.targetLabel.text = textField.text;

    [self.targetLabel sizeToFit];

    

    [textField resignFirstResponder];

    return NO;

}

– (void)moveText:(UIPanGestureRecognizer*)pgr

{

    // panで動かす

    pgr.view.center = [pgr locationInView:self.view];

}

– (void)openImagePicker

{

    // photo library から選ぶ

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

    [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

    [imagePicker setDelegate:self];

    [self presentViewController:imagePicker animated:YES completion:nil];

}

– (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    // image picker で選んだ写真を表示する

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    [self.pictureView setImage:image];

    [self dismissViewControllerAnimated:YES completion:nil];

}

– (void)saveImage

{

    // photo libraryに保存する。

    UIGraphicsBeginImageContext(self.pictureView.frame.size);

    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, NULL);

    UIGraphicsEndImageContext();

    

    

    // shutter effectを付けてみる

    UIView *stop = [[UIView alloc] initWithFrame:CGRectMake(0, 500, 320, 500)];

    UIView *sbottom = [[UIView alloc] initWithFrame:CGRectMake(0, –500, 320, 500)];

    stop.backgroundColor = [UIColor blackColor];

    sbottom.backgroundColor = [UIColor blackColor];

    [self.view addSubview:stop];

    [self.view addSubview:sbottom];

    

    [UIView animateWithDuration:0.3 animations:^{

        stop.center = self.view.center;

        sbottom.center = self.view.center;

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.3 animations:^{

            stop.center = CGPointMake(160, 800);

            sbottom.center = CGPointMake(160, –500);

        } completion:^(BOOL finished) {

            [stop removeFromSuperview];

            [sbottom removeFromSuperview];

        }];

    }];

    

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end