触摸绘图不透明度

时间:2015-04-28 20:17:34

标签: ios objective-c core-graphics

我正在尝试制作一个可以控制画笔不透明度的绘图应用程序但是当我尝试降低不透明度时,结果就像这样 - Picture。 我使用核心图形。如何解决这个问题?

我的一些代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.imageViewProperty];}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.imageViewProperty];

UIGraphicsBeginImageContext(self.imageViewProperty.frame.size);
[self.imageViewProperty.image drawInRect:CGRectMake(0, 0, self.imageViewProperty.frame.size.width, self.imageViewProperty.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), redColorValue, greenColorValue, blueColorValue, alphaValue);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.imageViewProperty.image = UIGraphicsGetImageFromCurrentImageContext();
[self.imageViewProperty setAlpha:1.0f];
UIGraphicsEndImageContext();
lastPoint = currentPoint;}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

if(!mouseSwiped) {
    UIGraphicsBeginImageContext(self.view.frame.size);
    [self.imageViewProperty.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), redColorValue, greenColorValue, blueColorValue, alphaValue);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    CGContextFlush(UIGraphicsGetCurrentContext());
    self.imageViewProperty.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

UIGraphicsBeginImageContext(self.mainImageProperty.frame.size);
[self.mainImageProperty.image drawInRect:CGRectMake(0, 0, self.mainImageProperty.frame.size.width, self.mainImageProperty.frame.size.height)
                               blendMode:kCGBlendModeNormal
                                   alpha:1.0];
self.mainImageProperty.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();}

0 个答案:

没有答案