简体   繁体   English

UIImageView使用图像的大小而不是图像视图来绘制矩形

[英]UIImageView draw in rect with size of image and not image view

I want to draw on UIImageView , following is my code for drawing. 我想在UIImageView上进行绘制,以下是我的绘制代码。 My image view mode is aspect fit. 我的图像查看模式适合宽高比。 When i draw using the following code, since i am using drawinrect and giving the rect of UIImageView so all the images are scaled down to the size to the `imageview. 当我使用以下代码进行绘制时,由于我正在使用drawinrect并提供UIImageView的rect,因此所有图像均按比例缩小到`imageview的大小。 I want to draw the image of size of image and not image view.Since i am detecting the touch point on the imageview so the point of tap and actual drawing on image is different. 我想绘制的图像大小为图像大小而不是图像视图。由于我正在检测图像视图上的接触点,因此图像上的点击和实际绘制点有所不同。 Can any body tell me how to draw on image directly. 谁能告诉我如何直接在图像上绘画。 and how to calculate this shift or difference in the point of touch on image rather than image view. 以及如何计算图像(而非图像视图)上的触摸点的这种偏移或差异。

    -(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;


 }

You shouldn't really be us classing UIImageView . 您不应该真的对我们分类UIImageView It isn't intended to be subclassed. 它不打算被子类化。

A better approach to this would be to have a UIView instead of the UIImageView . 更好的方法是使用UIView而不是UIImageView

Then two possible ways is to either... 那么两种可能的方法是...

Draw the UIImage directly into the UIView inside drawRect but this will give you the same problem that you are currently having. UIImage直接绘制到drawRect内部的UIView ,但这将给您当前遇到的相同问题。

Or... 要么...

In the UIView have a UIImageView . UIView有一个UIImageView Resize this so that it is the correct size/shape for the UIImage . 调整大小,使其是UIImage的正确大小/形状。 (Ie do the scaling yourself) then over the UIImageView put a second UIView (and subclass this) this second one will be capped DrawingView or something. (即自行缩放),然后在UIImageView放置第二个UIView (并将其子类化),第二个UIView会以DrawingView或其他内容为上限。 You can now do all your drawing inside here. 现在,您可以在此处进行所有绘图。 And all your touch detection will be in here too. 您的所有触摸检测也将在这里。 So you no longer need to convert the touch points into different drawing points. 因此,您不再需要将接触点转换为不同的绘图点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM