0

I am trying to make a drawing app that has a control on the opacity of the brush but when I tried to lower the opacity the result is like this - Picture. I used core graphics. How to fix this problem?

Some my code:

- (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();}
olezko46
  • 17
  • 5
  • 1
    I think what's happening is that where the lines overlap, the pixels are composited using the blend mode... and you end up with a visibly darker result. There may be a different CGBlendMode that'll give you the desired result. If not, you could try collecting all the touch samples, then draw a single line with multiple points, rather than lots of small lines? – mattdaw Apr 28 '15 at 21:51
  • I agree with the previous comment. The darker areas are where the line segments intersect. You could try changing the endcap (where the line continues), but I think drawing a single line would be better. It's really the endocaps that are intersecting. – Victor Engel Apr 29 '15 at 03:04
  • See http://stackoverflow.com/questions/6262427/how-to-get-rid-of-this-points-between-my-lines-when-i-am-drawing Use kCGLineCapButt if you want the linecap solution. – Victor Engel Apr 29 '15 at 03:06

0 Answers0