【发布时间】:2024-01-08 20:38:01
【问题描述】:
我在我的应用中实现了一个高亮功能。此高亮显示在 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;
}
【问题讨论】:
-
没有看到您的代码,很难帮助您。您需要编辑您的问题以包含获取触摸坐标并绘制突出显示的代码。
-
嗨。我添加了上面的代码。请帮助我知道代码有什么问题。谢谢。
标签: ios cocoa-touch uiimage core-animation core-graphics