objective-c - 在 UIImageView 的图像 : which rect to use? 上调用 drawInRect

标签 objective-c cocoa-touch ios uiimageview quartz-graphics

我正在尝试使用 Quartz 图形编写一个手指画类型的程序。我遇到的问题是屏幕旋转时。我画出的点和线“爬行”远离我触摸的地方。每次连续触摸,前一个点都会慢慢消失。这在两种纵向模式中都不会发生,但在两种横向模式中都会发生。

我可能弄错了,但我认为我的问题在于这个电话:

[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height)];
drawImageUIImageView在我的主视图 Controller 的 nib 文件中。

我也尝试过使用以下内容:
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height  )];

[drawImage.image drawInRect:drawImage.bounds];

[drawImage.image drawInRect:self.view.bounds];

我想我真的不知道是否使用self或我的UIImageView此调用的对象。

我都试过了,在 drawInRect调用并在哪里设置我的绘图上下文(保持一致)。

我在 viewDidLoad 中设置了这样的上下文(尝试创建一个持久的上下文,我不必在每次绘制时都重新设置 - 使用推送和弹出上下文)。
//---------------- set up a persistent context for drawing -----------------

UIGraphicsBeginImageContext(self.view.frame.size);
context = UIGraphicsGetCurrentContext();
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, curWidth);
CGContextSetRGBStrokeColor(context, curRed, curGreen, curBlue, curAlpha);
UIGraphicsPushContext(context);

我试图摆脱我在旋转时设置的上下文并在 View 旋转时重新初始化它,认为这是问题所在,但我不确定我做对了。

最佳答案

您不应该直接自己绘制该图像。即使您设置了自己的上下文并正确地绘制到其中,您也必须以某种方式将其刷新到屏幕上。这就是 UIKit 在调用 drawRect: 之前为你设置上下文的原因。关于意见。它已经准备好在屏幕上放东西了,它只需要知道放什么。

您可以设置自己的图像上下文并在 touchesBegan: 中绘制。等,但你应该做的是使用 UIGraphicsGetImageFromCurrentImageContext 将其转换为图像, 调用[myImageView setImage:theImageIJustGot] ,然后是 [myImageView setNeedsDisplay] .这向 UIKit 发出信号,表明你有新的东西要放在屏幕上,而 UIKit 会处理剩下的事情。

关于objective-c - 在 UIImageView 的图像 : which rect to use? 上调用 drawInRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6171461/

相关文章:

ios - 如何使 UIbutton 仅在写入文本时出现?

ios - 选择不同部分中的行时如何显示错误消息

iphone - CGPathAddEllipseInRect 的替代方案

ios - 企业 ios 应用程序中的崩溃日志

iphone - iOS 自定义字体

iOS同时推送BottomBar和ToolBar

iOS gamekit/蓝牙数据流

iphone - 更改产品名称会导致问题吗?

ios - QuickBlocks QBSettings 获取未声明的标识符

iphone - 正则表达式名称和姓氏