iphone - UIImageView 在矩形中绘制图像大小而不是 ImageView

标签 iphone objective-c ios cocoa-touch uiimageview

我想在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/

相关文章:

ios - 执行 UIAlertController 实例时,在单个文本字段对象上多次触发 textFieldShouldEndEditing 委托(delegate)方法

ios - 当我快速导入 OBJ-C 类时,Xcode 不会提示我添加 Bridging-header.h

iphone - UIImagePickerController 错误

iphone - iOS 应用未能及时启动 0x000000008badf00d - applicationDidBecomeActive 延迟

ios - 两个 viewController swift ios 中 uiwebview 的相同实例

ios - canDisplayBannerAds 导致功能不起作用?

ios - Xcode 6.3(和 6.2)在 [UIFont fontWithName : size:] 上命中断点

objective-c - AVCaptureVideoPreviewLayer方向错误

objective-c - objective-c : NSRange location value is not what it should be

objective-c - 删除 XCode 中的 OS X 应用程序工具栏