Ios UIImage中的图形正在被扭曲

Ios UIImage中的图形正在被扭曲,ios,cocoa-touch,uiimage,core-animation,core-graphics,Ios,Cocoa Touch,Uiimage,Core Animation,Core Graphics,我已经在我的应用程序中实现了突出显示功能。在UIImage中绘制此高光,以便将其保存为PNG表示形式。一切都很顺利,但最近我意识到了一个非常令人困惑的问题。有时,当我高亮显示时,图形被扭曲。下面是它的外观: 每当我移动手指高亮显示角色时,绘制的高亮显示都会向左延伸。另一个: 在这张照片中,每次我移动手指高亮显示时,绘制的高亮显示都会向上移动 这一切对我来说都很困惑。这种情况时有发生,有时只发生在某些页面上。有时,它工作得很好,就像这样: 我很困惑为什么会发生这种情况。有人能告诉我或者至少告

我已经在我的应用程序中实现了突出显示功能。在UIImage中绘制此高光,以便将其保存为PNG表示形式。一切都很顺利,但最近我意识到了一个非常令人困惑的问题。有时,当我高亮显示时,图形被扭曲。下面是它的外观:

每当我移动手指高亮显示角色时,绘制的高亮显示都会向左延伸。另一个:

在这张照片中,每次我移动手指高亮显示时,绘制的高亮显示都会向上移动

这一切对我来说都很困惑。这种情况时有发生,有时只发生在某些页面上。有时,它工作得很好,就像这样:

我很困惑为什么会发生这种情况。有人能告诉我或者至少告诉我为什么会发生这种情况吗?请帮帮我

代码:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   currPoint = [[touches anyObject]locationInView:self];

   for (int r = 0; r < [rectangles count]; r++) 
   {
      CGRect rect = [[rectangles objectAtIndex:r]CGRectValue];

      //Get the edges of the rectangles
      CGFloat xEdge = rect.origin.x + rect.size.width;
      CGFloat yEdge = rect.origin.y + rect.size.height;

    if ((currPoint.x > rect.origin.x && currPoint.x < xEdge) && (currPoint.y < rect.origin.y && currPoint.y > yEdge))
    {
        imgView.image = [self drawRectsToImage:imgView.image withRectOriginX:rect.origin.x originY:rect.origin.y rectWidth:rect.size.width rectHeight:rect.size.height];
        break;
    }
   }
}

//The function that draws the highlight
- (UIImage *)drawRectsToImage:(UIImage *)image withRectOriginX:(CGFloat)x originY:(CGFloat)y rectWidth:(CGFloat)width rectHeight:(CGFloat)ht
{
  UIGraphicsBeginImageContext(self.bounds.size);
  CGContextRef context = UIGraphicsGetCurrentContext();
  [image drawInRect:self.bounds];

  CGContextSetStrokeColorWithColor(context, [UIColor clearColor].CGColor);

  CGRect rect = CGRectMake(x, y, width, ht);
  CGContextAddRect(context, rect);
  CGContextSetCMYKFillColor(context, 0, 0, 1, 0, 0.5);
  CGContextFillRect(context, rect);

  UIImage *ret = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return ret;
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{
currPoint=[[触摸任何对象]位置查看:self];
对于(int r=0;r<[矩形计数];r++)
{
CGRect rect=[[矩形对象索引:r]CGRectValue];
//获取矩形的边
CGFloat xEdge=rect.origin.x+rect.size.width;
CGFloat yEdge=rect.origin.y+rect.size.height;
if((currPoint.x>rect.origin.x&&currPoint.xyEdge))
{
imgView.image=[self-drawRectsToImage:imgView.image WithRectoritiginx:rect.origin.x originY:rect.origin.y rectWidth:rect.size.width rectHeight:rect.size.height];
打破
}
}
}
//绘制高亮显示的函数
-(UIImage*)DrawRectToImage:(UIImage*)带目录项的图像x:(CGFloat)x原始:(CGFloat)y rectWidth:(CGFloat)width rectHeight:(CGFloat)ht
{
UIGraphicsBeginImageContext(self.bounds.size);
CGContextRef context=UIGraphicsGetCurrentContext();
[image drawInRect:self.bounds];
CGContextSetStrokeColorWithColor(上下文[UIColor clearColor].CGColor);
CGRect rect=CGRectMake(x,y,宽度,ht);
CGContextAddRect(上下文,rect);
CGContextSetCMYKFillColor(上下文,0,0,1,0,0.5);
CGContextFillRect(上下文,rect);
UIImage*ret=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
返回ret;
}

我不能确切地告诉您为什么会出现这些工件,但是

我认为在触摸处理器中渲染图像不是一个好主意。触摸操作应尽可能少

您可能想尝试使用CoreAnimation的
CALayer

像这样将图像设置为背景图像(假设您的类是
UIView
的子类)或使用实际的
UIImageView

self.layer.contents = (id)image.CGImage;
当您检测到另一个矩形
rect
已被触摸时,请在图像背景上方添加高亮显示作为子层:

CALayer *highlightLayer = [CALayer layer];
highlightLayer.frame = rect;
highlightLayer.backgroundColor = [UIColor yellowColor].CGColor;
highlightLayer.opacity = 0.25f;
[self.layer addSublayer:highlightLayer];
必要时,
shouldRasterize
图层属性可能有助于提高性能:

self.layer.shouldRasterize = YES;
为了使用所有这些,请使用QuartzCore框架并在实现文件中导入


您可以通过将
self.layer
渲染到图像上下文来创建层层次结构的PNG表示,然后获取图像和PNG表示。

如果不查看代码,很难为您提供帮助。您需要编辑您的问题,以包括获取触摸坐标和绘制突出显示的代码。Hi。我在上面添加了代码。请帮我知道代码出了什么问题。谢谢,你好,瑞玛。谢谢你的回复。自从我发布这个问题以来,我就从使用UIImage改为使用CALayer。我可以说,我终于可以更好地突出单词了。我只是想知道是什么导致了这些图纸的失真。无论如何,谢谢你的回答。我将投票表决