Ios 使用Quartz 2d/CGContext和缩放绘制时出现问题
现在我们有一个UIScrollView,它有一个子视图,一个UIView。此UIView包含两个UIImageView子视图,一个是静态背景图像。第二个UIImageView是供用户使用的视图,我们使用CGContext处理它 我们使用滚动视图是因为背景图像和绘图区域很大。当用户选择进入绘图模式的按钮时,他们可以用两个手指滑动以滚动,并使用一个手指进行绘图。这一切都很好 问题是当我们离开绘图模式并尝试放大时。背景图像可以完美地缩放,但它上面的图形UIImageView却做了一些奇怪的事情。它将保持正常大小几秒钟,然后消失,然后突然以放大的大小出现。所以它正在放大,但是有一些非常奇怪的行为和延迟 以下是我们处理图纸的方法,以供参考Ios 使用Quartz 2d/CGContext和缩放绘制时出现问题,ios,ipad,ios4,uiscrollview,quartz-2d,Ios,Ipad,Ios4,Uiscrollview,Quartz 2d,现在我们有一个UIScrollView,它有一个子视图,一个UIView。此UIView包含两个UIImageView子视图,一个是静态背景图像。第二个UIImageView是供用户使用的视图,我们使用CGContext处理它 我们使用滚动视图是因为背景图像和绘图区域很大。当用户选择进入绘图模式的按钮时,他们可以用两个手指滑动以滚动,并使用一个手指进行绘图。这一切都很好 问题是当我们离开绘图模式并尝试放大时。背景图像可以完美地缩放,但它上面的图形UIImageView却做了一些奇怪的事情。它将保
/**
When the user first starts moving the pen
*/
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
isMouseMoved = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:imageView];
// Setting up the context
UIGraphicsBeginImageContext(imageView.frame.size);
[imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
if(isEraserOn){
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), eraserRadius);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
CGRect eraserFrame = eraser.frame;
eraserFrame.origin.x = currentPoint.x - (eraserRadius/2);
eraserFrame.origin.y = currentPoint.y - (eraserRadius/2);
eraser.frame = eraserFrame;
}else{
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), penRadius);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal);
}
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 1) {
mouseMoved = 0;
}
}
有人知道我们的绘图视图是怎么回事吗?我们认为延迟可能是由于放大时的抗锯齿造成的,但我们尝试禁用该功能,但仍然遇到相同的问题。问题可能是,您在TouchsMoved方法中进行所有绘图,而不是在DrawRect中进行,并在需要时调用setNeedsDisplay方法调用绘图。在一个优秀的视频中,它已经清楚地解释了在touchesMoved或tochesbearth等方法中获取CGContext是与绘图相关的问题的最常见原因之一。所以我建议你: 1不要在tochesMoved中调用UIGraphicsGetCurrentContext,而是在用户绘制的UIView的DrawRect方法中调用。在TouchsMoved中,您只需保存已触摸的点,也可以在DrawRect中绘制线 2在触摸移动结束时向UIView发送设置需要显示信息。这将确保每次调用touchesMoved时UIView都会刷新,并且可能会缩短响应时间。你可以尝试一下,甚至不用尝试1
希望这有帮助 问题可能在于,您在touchesMoved方法中进行所有绘图,而不是在DrawRect中进行,并在需要时调用setNeedsDisplay方法来调用绘图。在一个优秀的视频中,它已经清楚地解释了在touchesMoved或tochesbearth等方法中获取CGContext是与绘图相关的问题的最常见原因之一。所以我建议你: 1不要在tochesMoved中调用UIGraphicsGetCurrentContext,而是在用户绘制的UIView的DrawRect方法中调用。在TouchsMoved中,您只需保存已触摸的点,也可以在DrawRect中绘制线 2在触摸移动结束时向UIView发送设置需要显示信息。这将确保每次调用touchesMoved时UIView都会刷新,并且可能会缩短响应时间。你可以尝试一下,甚至不用尝试1 希望这有帮助