我目前正在构建一个绘画应用。我收到一个奇怪的错误,我昨天在代码中没有收到:
Incompatible pointer types passing retainable parameter of type 'CFMutableDataRef' (aka 'struct __CFData *')to a CF function expecting 'CGPathRef' (aka 'const struct CGPath *') type
这些行发生错误:
CGContextAddPath(context, tempLine.linePath);
CGContextAddPath(context, self.currentLine.linePath);
CGPathMoveToPoint(self.currentLine.linePath, NULL, cPoint.x, cPoint.y);
CGPathAddLineToPoint(self.currentLine.linePath, NULL, currentPoint.x, currentPoint.y);
CGPathAddLineToPoint(self.currentLine.linePath, NULL, cPoint.x, cPoint.y);
我已导入,但它似乎无法正常工作。
这是我的代码:
@implementation CanvasView
@synthesize lines, currentLine;
-(id)initWithFrame:(CGRect)frame{
if ((self = [super initWithFrame:frame])) {
self.backgroundColor = [UIColor clearColor];
self.currentLine = [[Line alloc] init];
self.lines = [NSMutableArray arrayWithCapacity:10];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsBeginImageContext(self.frame.size);
// draw accumulated lines
if ([self.lines count] > 0) {
for (Line *tempLine in self.lines){
CGContextSetAlpha(context, tempLine.opacity);
CGContextSetStrokeColorWithColor(context, tempLine.lineColor.CGColor);
CGContextSetLineWidth(context, tempLine.lineWidth);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextAddPath(context, tempLine.linePath);
CGContextStrokePath(context);
}
}
//draw current line
CGContextSetAlpha(context, self.currentLine.opacity);
CGContextSetStrokeColorWithColor(context, self.currentLine.lineColor.CGColor);
CGContextSetLineWidth(context, self.currentLine.lineWidth);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextBeginPath(context);
CGContextAddPath(context, self.currentLine.linePath);
CGContextStrokePath(context);
UIGraphicsEndImageContext();
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint cPoint = [touch locationInView:self];
CGPathMoveToPoint(self.currentLine.linePath, NULL, cPoint.x, cPoint.y);
[self setNeedsDisplay];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
CGPathAddLineToPoint(self.currentLine.linePath, NULL, currentPoint.x, currentPoint.y);
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint cPoint = [touch locationInView:self];
CGPathAddLineToPoint(self.currentLine.linePath, NULL, cPoint.x, cPoint.y);
[self setNeedsDisplay];
[self.lines addObject:self.currentLine];
Line *nextLine = [[Line alloc] initWithOptions:self.currentLine.lineWidth color:self.currentLine.lineColor opacity:self.currentLine.opacity];
self.currentLine = nextLine;
NSLog(@"touch #: %u", [self.lines count]);
}
@end