gpt4 book ai didi

ios - 使用 Quartz 2d/CGContext 绘图和缩放的问题

转载 作者:行者123 更新时间:2023-12-01 17:01:11 25 4
gpt4 key购买 nike

现在我们有一个 UIScrollView,它有一个 subview ,一个 UIView。这个 UIView 拥有两个 UIImageView subview ,一个是静态背景图片。第二个 UIImageView 是一个供用户绘制的 View ,我们使用 CGContext 处理。

我们使用 ScrollView 是因为背景图像和绘图区域很大。当用户选择一个进入绘图模式的按钮时,他们可以用两根手指滑动滚动并用一根手指进行绘图。这一切都很好。

问题是当我们离开绘图模式并尝试放大时。背景图像完美缩放,但在它上面绘制的 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;
}
}

有谁知道我们的绘图 UIImageView 发生了什么?我们认为延迟可能是由于放大时的抗锯齿,但我们尝试禁用它,但仍然遇到同样的问题。

最佳答案

问题可能在于,您在 touchesMoved 方法中进行所有绘图,而不是在 DrawRect 中进行,并在需要时调用 setNeedsDisplay 方法来调用绘图。在 wwdc 2011 的精彩视频中已经清楚地解释了在诸如 touchesMoved 或 tochesBegan 之类的方法中获取 CGContext 是与绘图相关的问题的最常见原因之一。所以我建议你:
1) 不要在 tochesMoved 中调用 UIGraphicsGetCurrentContext(),而是在用户绘制的 UIView 的 DrawRect 方法中调用。在 touchesMoved 中,您可以简单地保存被触摸的点,您也可以在 DrawRect 中绘制线条
2) 在 touchesMoved 结束时向 UIView 发送 setNeedsDisplay 消息。这将确保每次调用 touchesMoved 时 UIView 都会刷新,并且可能会缩短响应时间。你甚至可以不试一试 (1)

希望这可以帮助!

关于ios - 使用 Quartz 2d/CGContext 绘图和缩放的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6817582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com