在UIImageView上用手指绘图,不覆盖整个屏幕

时间:2014-11-11 18:58:28

标签: ios objective-c uiimageview

我已经设法在最近捕获的UIImage工作之上绘图,但它仅在UIImageView覆盖整个屏幕时才有效。当我开始从水平中心拉开时,它开始失去准确性。任何想法我怎么能让它只在ImageView本身工作并保持准确?

- (void)viewDidLoad
{
  [super viewDidLoad];

  float height = (self.view.frame.size.width * self.chosenImage.size.height)/self.chosenImage.size.width;

  self.imageView.frame = CGRectMake(0, self.navigationController.navigationBar.frame.size.height, self.view.frame.size.width, height);
  self.imageView.center = self.imageView.superview.center;


  self.imageView.image = self.chosenImage; 
}

- (UIImage *)drawLineFromPoint:(CGPoint)from_Point toPoint:(CGPoint)to_Point image:(UIImage *)image
{
  CGSize sizeOf_Screen = self.imageView.frame.size;
  UIGraphicsBeginImageContext(sizeOf_Screen);
  CGContextRef current_Context = UIGraphicsGetCurrentContext();
  [image drawInRect:CGRectMake(0, 0, sizeOf_Screen.width, sizeOf_Screen.height)];

  CGContextSetLineCap(current_Context, kCGLineCapRound);
  CGContextSetLineWidth(current_Context, 1.0);
  CGContextSetRGBStrokeColor(current_Context, 1, 0, 0, 1);
  CGContextBeginPath(current_Context);
  CGContextMoveToPoint(current_Context, from_Point.x, from_Point.y);
  CGContextAddLineToPoint(current_Context, to_Point.x, to_Point.y);
  CGContextStrokePath(current_Context);

  UIImage *rect = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return rect;
}

- (void)touchesBegan:(NSSet *)_touches withEvent:(UIEvent *)_event
{
  // retrieve the touch point
  UITouch *_touch = [_touches anyObject];
  CGPoint current_Point = [_touch locationInView:self.imageView];

  // Its record the touch points to use as input to our line smoothing algorithm
  self.drawnPoints = [NSMutableArray arrayWithObject:[NSValue valueWithCGPoint:current_Point]];

  self.previousPoint = current_Point;

  // we need to save the unmodified image to replace the jagged polylines with the smooth polylines
  self.cleanImage = self.imageView.image;
}

- (void)touchesMoved:(NSSet *)_touches withEvent:(UIEvent *)_event
{

  UITouch *_touch = [_touches anyObject];
  CGPoint current_Point = [_touch locationInView:self.imageView];

  [self.drawnPoints addObject:[NSValue valueWithCGPoint:current_Point]];

  self.imageView.image = [self drawLineFromPoint:self.previousPoint toPoint:current_Point image:self.imageView.image];

  self.previousPoint = current_Point;
}

编辑:通过这些设置,我在UIImageView中具有准确性,但在我绘图时图像会向内模糊。

当我编辑[image drawInRect:CGRectMake(0,0,sizeOf_Screen.width,sizeOf_Screen.height)]; to [image drawInRect:CGRectMake(0,self.imageView.frame.origin.y,sizeOf_Screen.width,sizeOf_Screen.height)];并尝试绘制,当我尝试绘制时,图像从视图中飞出。

我尝试在drawLineFromPoint方法中调整sizeOf_screen值和CGRectMake图像。我也尝试将两个实例中的locationInView设置为self.imageView,但无济于事。这很奇怪。任何可能完成我正在寻找的想法或代码示例?我似乎找不到任何东西。

0 个答案:

没有答案