iphone: rewrite GLPaint using Core Graphics

2019-06-14 05:53发布

问题:

I would like to rewrite the Apple source code, GLPaint application, but not using OpenGL like the example, I would like to use Core Graphics library. But I stuck in displaying the texture like GLPaint.

This is my code in Core Graphics:

- (void) drawLineFromPoint: (CGPoint) fromPoint toPoint: (CGPoint) toPoint {
    UIGraphicsBeginImageContext(self.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [drawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    UIImage * image = [UIImage imageNamed:@"Particle.png"];
    UIColor * color = [UIColor colorWithPatternImage:image];
    CGContextSetStrokeColorWithColor(context, color.CGColor);

    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, 5.0);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, fromPoint.x, fromPoint.y);
    CGContextAddLineToPoint(context, toPoint.x, toPoint.y);
    CGContextStrokePath(context);
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

--- But I receive this result:

--- I would like to have this result:

Does my code wrong? Any one can help me on this? Thank you very much.

回答1:

If you don't want the pattern, don't set a colour with a pattern image as your stroke colour.

Instead of

UIImage * image = [UIImage imageNamed:@"Particle.png"];
UIColor * color = [UIColor colorWithPatternImage:image];

Just do

UIColor *color = [UIColor blackColor];


回答2:

Kindly make sure you have the Particle.png added to the project. Also check the constants kBrushScale and kPalette* in GLPaint code.