写真をタッチすると、
たくさんの小さな写真に分裂、
フォトモザイク(?)のできあがり。
というiPhoneアプリのサンプルを作ってみました。

ポイント
UIGraphicsImageContextを使って、小さな写真にする部分の
画像をUIImageとして抜き出しています。


サンプルコード


#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () {

    UIImageView *photo;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blackColor];

    [self loadImage];

}

– (void)loadImage

{

    UIImage *img = [UIImage imageNamed:@”image”];

    photo = [[UIImageView alloc] initWithImage:img];

    photo.contentMode = UIViewContentModeScaleAspectFit;

    photo.frame = self.view.bounds;

    photo.backgroundColor = [UIColor blackColor];

    [self.view addSubview:photo];

}

– (void)randomCutting

{

    for (int i = 0; i<36; i++) {

        float x = (i%6) * 40 + (arc4random() % 30) + 20;

        float y = (i/6) * 40 + (arc4random() % 30) + 140;

        float size = (arc4random() % 20) + 40;

        

        CGRect rect = CGRectMake(x, y, size, size);

        float angle = (arc4random() % 10) * M_PI * 0.01;

        UIImage *img = [self cutImage:rect angle:angle];

        UIImageView *cutimg = [[UIImageView alloc] initWithImage:img];

        cutimg.frame = rect;

        cutimg.layer.borderWidth = 2;

        cutimg.layer.borderColor = [UIColor whiteColor].CGColor;

        cutimg.layer.shadowColor = [UIColor whiteColor].CGColor;

        cutimg.layer.shadowOffset = CGSizeMake(1, 1);

        cutimg.layer.shadowRadius = 3;

        cutimg.layer.shadowOpacity = 0.8;

        

        [self.view addSubview:cutimg];

        cutimg.userInteractionEnabled = YES;

        cutimg.transform = CGAffineTransformMakeScale(0, 0);

        

        [UIView animateWithDuration:0.1 delay:i*0.01 options:UIViewAnimationOptionCurveEaseIn animations:^{

            cutimg.transform = CGAffineTransformMakeRotation(angle);

        } completion:^(BOOL finished) {}];

    }

}

– (UIImage*)cutImage:(CGRect)frame angle:(float)angle

{

    UIGraphicsBeginImageContext(frame.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextRotateCTM(ctx, -angle);

    CGContextTranslateCTM(ctx, -frame.origin.x, -frame.origin.y);

    [photo.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    return outputImage;

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    static BOOL flag = YES;

    if (flag) {

        flag = NO;

        [self randomCutting];

        [UIView animateWithDuration:1.0 animations:^{

            photo.alpha = 0.0;

        }];

    } else {

        photo.alpha = 1;

        for (UIView *v in self.view.subviews) {

            if (v != photo) {

                [v removeFromSuperview];

            }

        }

        flag = YES;

    }

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end