ios - 在 uiimageview 上使用 Quartz 2D 绘图 - 线条变得模糊

标签 ios xcode drawing core-graphics cgcontext

这是一个与在 iPad 应用程序 View 中使用 Core Graphics 绘制路径(响应触摸事件)相关的问题。

我有一个 View 作为 ScrollView 的一部分,我想在其中跟踪触摸路径。下面的代码适用于应用程序中的其他 uiimageview,但不适用于这个特定的 subview 。当我绘制时(通过在屏幕上描摹/触摸),线条出现但开始沿 y 方向移动。所以,整个轨迹在 y 方向上不断压缩!!随着线条向上移动,它们似乎也在扩散并最终消失!

对于 ScrollView 之外的不同 View ,相同的代码工作正常。在过去的 3 天里,我一直在网上搜索并花了几个小时试图了解这里发生了什么,但我仍然一无所知。任何帮助/建议将不胜感激。

谢谢。

这是我的绘图代码:

UIGraphicsBeginImageContext(coverIVFragments.frame.size);
            [coverIVFragments.image drawInRect:CGRectMake(0, 0, coverIVFragments.frame.size.width, coverIVFragments.frame.size.height)];
            CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
            CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
            CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
            CGContextBeginPath(UIGraphicsGetCurrentContext());
            CGContextMoveToPoint(UIGraphicsGetCurrentContext(), x1, y1);
            CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), x2, y2);
            CGContextStrokePath(UIGraphicsGetCurrentContext());

            coverIVFragments.image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();

最佳答案

首先,您应该使用采用比例而不是 UIGraphicsBeginImageContext 的新方法(在 iPad 上没有 Xcode 方便)。

问题可能出在与 ScrollView 的交互上。在例程的顶部记录 ScrollView contentOffset 和您的框架。您一直假设原点为 0,0,但实际上您想要的框架很可能是 sv.contentOffset 和 sv.bounds.size 的组合。

对我来说,我经常记录这种问题,很快我需要做什么就会变得很明显。

编辑:您的问题是使用非整数矩形 - 此行更正了没有缩放的问题:

   coverImageView.frame = CGRectIntegral(coverImageView.frame);
   UIGraphicsBeginImageContextWithOptions(coverImageView.frame.size, NO, imageScrollView.zoomScale);

我认为您没有正确使用“scale”参数 - 可能您应该调整绘图将进入的矩形的大小,并仅将 scale 用于其预期目的并在此处传递“0”。

关于ios - 在 uiimageview 上使用 Quartz 2D 绘图 - 线条变得模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12439686/

相关文章:

ios - Xcode 6 & Swift - 缩进反转?

ios - 如何防止 UIAlertView 自动旋转?

ios - 初始导入后 UITableview 在 iOS 10 中不再刷新

ios - 无法从我的协议(protocol)类调用委托(delegate)方法

ios - Storyboard附加 View 到代码 xcode 5.1

ios - 在当前行显示错误的 Xcode 快捷方式

swift - 更漂亮的调试输出在 Xcode 中打印 Swift 字典

c# - 在 WPF 的主窗口上绘画

objective-c - 优化view绘制代码

c# - 如何截屏 "preview"窗口?