在iOS中沿着path均匀地绘制图像

我想要做的就是在屏幕上移动我的手指(touchesMoved)并沿touchesMoved生成的点绘制均匀间隔的图像(可能是CGImageRefs)。 我可以画线,但是我想要生成的东西看起来像这样(对于这个例子,我使用的是一个箭头的图像,但它可能是任何图像,可能是我的狗的图片:))主要的是当用iPhone或iPad上的手指进行绘图时,图像均匀分布。

在这里输入图像说明

假设你已经有了跟踪用户触摸的代码,当他们在屏幕上移动他们的触摸时,这听起来像你想要检测他们已经移动了一个等于图像长度的距离,在这个时候你想绘制另一个副本你的形象在他们的触摸下。

为了达到这个目的,我认为你需要:

  • 计算图像的长度(宽度)
  • 实现代码将您的图像的副本绘制到您的视图,旋转到任何angular度
  • 每次用户的触摸移动(例如在touchesMoved :):
    • 计算每次移动的触摸增量,并生成该增量的“长度”(例如sqrt(dx ^ 2 + dy ^ 2))
    • 累积距离最后一幅图像的距离
    • 如果距离已达到图像的长度,请在触摸的当前位置下绘制图像副本,并进行适当旋转(可能根据从最后一张图像位置到当前位置的vector)

听上去怎么样?

首先巨大的道具出去肯德尔。 所以,根据他的回答,这里是取UIImage的代码,根据触摸之间的距离在屏幕上沿着path(不是真正的path引用,只是由点创build的逻辑path)在屏幕上绘制代码,然后旋转图像正确地基于当前点和前一点的VECTOR。 我希望你喜欢它:

首先你需要加载一个图像作为CGImage反复使用:

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"arrow.png" ofType:nil]; UIImage *img = [UIImage imageWithContentsOfFile:imagePath]; image = CGImageRetain(img.CGImage); 

确保在你的dealloc中你打电话

  CGImageRelease(image); 

然后在touchesBegan中,只是将起点存储在一个var的范围外的方法(在这种情况下声明在这样的头:)在这种情况下,我正在绘制成一个UIView

 @interface myView : UIView { CGPoint lastPoint; } @end 

然后触动开始:

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

{

 UITouch *touch = [touches anyObject]; lastPoint = [touch locationInView:self]; 

}

最后在touchesMoved,绘制位图到屏幕上,然后当你的距离已经足够移动(在我的情况73,因为我的图像是73像素×73像素)绘制该图像到屏幕上,保存新的图像,并设置lastPoint相等到currentPoint

  -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; currentPoint = [touch locationInView:self]; double deltaX = lastPoint.x - currentPoint.x; double deltaY = lastPoint.y - currentPoint.y; double powX = pow(deltaX,2); double powY = pow(deltaY,2); double distance = sqrt(powX + powY); if (distance >= 73){ lastPoint = currentPoint; UIGraphicsBeginImageContext(self.frame.size); [drawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; CGContextSaveGState(UIGraphicsGetCurrentContext()); float angle = atan2(deltaX, deltaY); angle *= (M_PI / 180); CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(currentPoint.x, currentPoint.y, 73, 73),[self CGImageRotatedByAngle:image angle:angle * -1]); CGContextRestoreGState(UIGraphicsGetCurrentContext()); drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); distance = 0; } } - (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(CGFloat)angle { CGFloat angleInRadians = angle * (M_PI / 180); CGFloat width = CGImageGetWidth(imgRef); CGFloat height = CGImageGetHeight(imgRef); CGRect imgRect = CGRectMake(0, 0, width, height); CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians); CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef bmContext = CGBitmapContextCreate(NULL, rotatedRect.size.width, rotatedRect.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst); CGContextSetAllowsAntialiasing(bmContext, FALSE); CGContextSetInterpolationQuality(bmContext, kCGInterpolationNone); CGColorSpaceRelease(colorSpace); CGContextTranslateCTM(bmContext, +(rotatedRect.size.width/2), +(rotatedRect.size.height/2)); CGContextRotateCTM(bmContext, angleInRadians); CGContextTranslateCTM(bmContext, -(rotatedRect.size.width/2), -(rotatedRect.size.height/2)); CGContextDrawImage(bmContext, CGRectMake(0, 0, rotatedRect.size.width, rotatedRect.size.height), imgRef); CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext); CFRelease(bmContext); [(id)rotatedImage autorelease]; return rotatedImage; } 

这将创build一个像这样的图像:

在这里输入图像说明

要添加以下内容(对上面的代码进行一些更改,以尝试填充touchesMoved在快速移动时缺less的某些点的空白处:

 CGPoint point1 = CGPointMake(100, 200); CGPoint point2 = CGPointMake(300, 100); double deltaX = point2.x - point1.x; double deltaY = point2.y - point1.y; double powX = pow(deltaX,2); double powY = pow(deltaY,2); double distance = sqrt(powX + powY); distance = 0; for (int j = 1; j * 73 < distance; j++ ) { double x = (point1.x + ((deltaX / distance) * 73 * j)); double y = (point1.y + ((deltaY / distance) * 73 * j)); NSLog(@"My new point is x: %fy :%f", x, y); }