iphone: rewrite GLPaint using Core Graphics

柔情痞子 提交于 2019-12-08 09:58:21

问题


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.



来源:https://stackoverflow.com/questions/6593566/iphone-rewrite-glpaint-using-core-graphics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!