我无法确切地告诉你为什么会出现这些文物,但......
我不认为在触摸处理程序中渲染图像是个好主意。触摸处理应该尽可能少。
您可能想尝试使用CoreAnimation CALayer :
CALayer
将您的图像设置为这样的背景图像(假设您的类是其子类 UIView )或使用实际的 UIImageView :
UIView
UIImageView
self.layer.contents = (id)image.CGImage;
当您检测到另一个矩形时 rect 已触摸,将突出显示添加为图像背景上方的子图层:
rect
CALayer *highlightLayer = [CALayer layer]; highlightLayer.frame = rect; highlightLayer.backgroundColor = [UIColor yellowColor].CGColor; highlightLayer.opacity = 0.25f; [self.layer addSublayer:highlightLayer];
该 shouldRasterize 如有必要,图层属性可能有助于提高性能:
shouldRasterize
self.layer.shouldRasterize = YES;
为了使用所有这些,使用QuartzCore框架并导入 <QuartzCore/QuartzCore.h> 在您的实现文件中。
<QuartzCore/QuartzCore.h>
您可以通过渲染来创建图层层次结构的PNG表示 self.layer 到图像上下文,然后获取图像和PNG表示。
self.layer