图像上的CGcontext绘图不起作用

时间:2021-02-13 23:13:38

This is my code and it makes a very weird drawing when executed. Moreover image starts to disappear slowly by going down the imageview. Please help me with this

这是我的代码,它在执行时会产生一个非常奇怪的图形。此外,通过沿着图像视图,图像开始慢慢消失。请在这件事上给予我帮助

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];

//    if ([touch tapCount] == 2)
//    {
//        imageView.image = nil;
//    }

location = [touch locationInView:touch.view];
lastClick = [NSDate date];

lastPoint = [touch locationInView:self.view];
lastPoint.y -= 0;

[super touchesBegan:touches withEvent:event];
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{mouseSwiped = YES;

UITouch *touch = [touches anyObject];
currentPoint = [touch locationInView:self.view];

UIGraphicsBeginImageContext(imageView.image.size);

[imageView.image drawInRect:CGRectMake(0, 44, imageView.image.size.width, imageView.image.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 1, 0, 1);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());

imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
 //   lastPoint = currentPoint;


}

Moreover, the lines its drawing are of weird shape and they are disappearing continously

而且,它绘制的线条形状怪异,并且它们不断消失

1 个解决方案

#1


0  

Your image is shifting, because you hardcoded offset in 44 points on each redraw.

您的图像正在移动,因为您在每次重绘时硬编码了44个点的偏移量。

Weird drawing is most likely the result of invalid coordinate system usage. You receive touch location in view coordinates, but draw in image coordinates. The easiest way to fix this issue is to create context with size, equal to view size instead of image size. Simply use imageView.bounds.size instead of imageView.image.size. Note that I assume you use "Scale to Fill" mode in your image view.

奇怪的绘图很可能是坐标系统使用无效的结果。您在视图坐标中接收触摸位置,但绘制图像坐标。解决此问题的最简单方法是创建大小等于视图大小而不是图像大小的上下文。只需使用imageView.bounds.size而不是imageView.image.size。请注意,我假设您在图像视图中使用“缩放到填充”模式。

Whole drawing code after changes:

更改后的整个绘图代码:

UIGraphicsBeginImageContext(self.imageView.bounds.size);

[self.imageView.image drawInRect:CGRectMake(0, 0, self.imageView.bounds.size.width, self.imageView.bounds.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 1, 0, 1);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.lastPoint.x, self.lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());

self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Also, your solution is not optimal in terms of performance. I suggest to draw path separately in view, instead of updating imageView image on each touch move.

此外,您的解决方案在性能方面并非最佳。我建议在视图中单独绘制路径,而不是在每次触摸移动时更新imageView图像。

#1


0  

Your image is shifting, because you hardcoded offset in 44 points on each redraw.

您的图像正在移动,因为您在每次重绘时硬编码了44个点的偏移量。

Weird drawing is most likely the result of invalid coordinate system usage. You receive touch location in view coordinates, but draw in image coordinates. The easiest way to fix this issue is to create context with size, equal to view size instead of image size. Simply use imageView.bounds.size instead of imageView.image.size. Note that I assume you use "Scale to Fill" mode in your image view.

奇怪的绘图很可能是坐标系统使用无效的结果。您在视图坐标中接收触摸位置,但绘制图像坐标。解决此问题的最简单方法是创建大小等于视图大小而不是图像大小的上下文。只需使用imageView.bounds.size而不是imageView.image.size。请注意,我假设您在图像视图中使用“缩放到填充”模式。

Whole drawing code after changes:

更改后的整个绘图代码:

UIGraphicsBeginImageContext(self.imageView.bounds.size);

[self.imageView.image drawInRect:CGRectMake(0, 0, self.imageView.bounds.size.width, self.imageView.bounds.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);

CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0, 1, 0, 1);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), self.lastPoint.x, self.lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());

self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Also, your solution is not optimal in terms of performance. I suggest to draw path separately in view, instead of updating imageView image on each touch move.

此外,您的解决方案在性能方面并非最佳。我建议在视图中单独绘制路径,而不是在每次触摸移动时更新imageView图像。