絵筆を使って指の動きでボケ効果を加えるアプリを作りたいです。これがコードです。
#import "fingerDrawView.h"
////create bokeh image
-(UIImage*)drawCircle{
///1) create a bitmap context
UIGraphicsBeginImageContext(self.bounds.size);
///2) get the context
CGContextRef circleContext = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(circleContext, 3.0f);
//circle1
CGContextSetFillColorWithColor(circleContext, [UIColor colorWithRed:0.5 green:1 blue:0.5 alpha:0.4].CGColor);
CGRect circle1Point = CGRectMake(0, 0, 80, 80);///// When play it in simulator, it look smaller than this size. I don’t know why???
CGContextFillEllipseInRect(circleContext, circle1Point);
CGContextSetStrokeColorWithColor(circleContext, [UIColor colorWithRed:0.3 green:0.9 blue:0 alpha:0.6].CGColor);
CGContextStrokeEllipseInRect(circleContext, circle1Point);
////4) export the context into an image
UIImage *circleImage = UIGraphicsGetImageFromCurrentImageContext();
//// 5) destroy the context
UIGraphicsEndImageContext();
return circleImage;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [touches anyObject];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
_imageBuffer = [self drawCircle];
dispatch_async(dispatch_get_main_queue(), ^{
CGPoint touchPoint = [touch locationInView:self];
CGPoint prev_touchPoint = [touch previousLocationInView:self];
if (ABS(touchPoint.x - prev_touchPoint.x) > 6
|| ABS(touchPoint.y - prev_touchPoint.y) > 6) {
_aImageView = [[UIImageView alloc]initWithImage:_imageBuffer ];
_aImageView.multipleTouchEnabled = YES;
_aImageView.userInteractionEnabled = YES;
[_aImageView setFrame:CGRectMake(touchPoint.x, touchPoint.y, 100.0, 100.0)];
[self addSubview:_aImageView];
}
});
});
}
シミュレーターで動作可能です。ただし、devise (ipad4) で実行するとクラッシュします。コンソールは「メモリ警告を受信しました」と通知しました。ボケ画像を描画するために GCD を作成しましたが、うまくいきませんでした。
ちなみにボケ画像のサイズは80X80(-(UIImage*)drawCircle)にしたいです。シミュレーターでプレイすると、このサイズよりも小さく見えます。