0

我想创建具有画笔的应用程序,以通过手指移动到图像上添加散景效果。这是代码。

#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];

            }

        });
    });


}

它可以在模拟器中工作。但是,它在设计(ipad4)中运行时崩溃。控制台通知“收到内存警告”。我制作了 GCD 来绘制散景图像,但它没有用。

顺便说一句,我想让散景图像大小为 80X80 (-(UIImage*)drawCircle)。在模拟器中播放时,它看起来比这个尺寸小。

4

0 回答 0