2

我正在使用以下方法绘制一条线:

- (void)drawLineWithColor:(UIColor *)color andRect: (CGRect)rect{

if (uploadButtonHidden == 2) {
    uploadPhotoButton.hidden = NO;
}

UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(context, 15, 110);

// set the stroke color and width
CGContextSetStrokeColorWithColor(context, [color CGColor]);
CGContextSetLineWidth(context, 6.0);

// move to your first point
CGContextMoveToPoint(context, 455, coords.y - 140);

// add a line to your second point
CGContextAddLineToPoint(context, coordsFinal.x, coordsFinal.y);

// tell the context to draw the stroked line
CGContextStrokePath(context);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// Draw image on view
[image drawAtPoint:CGPointZero];
}

如您所见, coords.y 设置了我的行的起点。更改 coords.y 点以更新我的线路时有什么办法吗?例如,如果我有一个每 0.5 秒向 coords.y 添加 50 的方法,我如何在不重绘的情况下更新该行(以防止内存崩溃)?

编辑:

这个方法是这样调用的:

UIGraphicsBeginImageContext(self.view.frame.size); 
[self drawLineWithColor:[UIColor   blackColor] andRect:CGRectMake(20, 30, 1476, 1965)]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
imageViewForArrows = [[UIImageView alloc] initWithImage:image];  
[imageArrowsArray addObject:imageViewForArrows];
4

1 回答 1

2

不要绘制到图像中,而是直接绘制到上下文中。我假设这个方法是从你的drawRect方法中调用的,在这种情况下已经有一个 value CGContextRef。你只需要得到它并吸引它。确保在应用变换或剪辑时使用CGContextSaveGState和。CGContextRestoreGState

- (void)drawLineWithColor:(UIColor *)color andRect: (CGRect)rect{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);
    CGContextTranslateCTM(context, 15, 110);

    // set the stroke color and width
    CGContextSetStrokeColorWithColor(context, [color CGColor]);
    CGContextSetLineWidth(context, 6.0);

    // move to your first point
    CGContextMoveToPoint(context, 455, coords.y - 140);

    // add a line to your second point
    CGContextAddLineToPoint(context, coordsFinal.x, coordsFinal.y);

    // tell the context to draw the stroked line
    CGContextStrokePath(context);
    CGContextRestoreGState(context);
}
于 2013-02-14T17:03:27.930 回答