我想在UIImageView
上绘图,以下是我的绘图代码。我的图像查看模式是宽高比适合。当我使用以下代码进行绘制时,由于我使用 drawinrect
并给出了 UIImageView
的矩形,因此所有图像都按比例缩小到“imageview”的大小。我想绘制图像大小的图像,而不是 ImageView 。由于我正在检测 ImageView 上的触摸点,因此点击点和图像上的实际绘制是不同的。谁能告诉我如何直接在图像上绘图。以及如何计算图像而不是 ImageView 上的触摸点的这种偏移或差异。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
lastTouch = [touch locationInView:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint currentTouch = [[touches anyObject] locationInView:self];
UIGraphicsBeginImageContext(self.image.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.image drawInRect:CGRectMake(0, 0, self.image.size.width, self.bounds.size.height)];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, _brushSize);
CGContextBeginPath(context) ;
CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
CGContextAddLineToPoint(context,currentTouch.x,currentTouch.y) ;
CGContextSetAlpha( context , 1 );
CGContextSetStrokeColorWithColor(context, [_brushColor CGColor]);
CGContextStrokePath(context) ;
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastTouch = currentTouch;
}
最佳答案
你不应该真正为我们分类UIImageView
。它不打算被子类化。
更好的方法是使用 UIView
而不是 UIImageView
。
那么有两种可能的方法是...
将UIImage
直接绘制到drawRect内的UIView
中,但这会给您带来与当前遇到的相同问题。
或者...
在UIView
中有一个UIImageView
。调整其大小,使其成为 UIImage
的正确大小/形状。 (即自己进行缩放)然后在 UIImageView
上放置第二个 UIView
(并对其进行子类化),第二个将被限制为 DrawingView 或其他内容。现在您可以在这里进行所有绘图。所有的触摸检测也将在这里。因此您不再需要将触摸点转换为不同的绘图点。
关于iphone - UIImageView 在矩形中绘制图像大小而不是 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13598871/