gpt4 book ai didi

ios - 在 iOS 中绘制沿路径均匀分布的图像

转载 作者:可可西里 更新时间:2023-11-01 04:23:04 25 4
gpt4 key购买 nike

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

enter image description here

最佳答案

首先,Kendall 获得了巨大的支持。因此,根据他的回答,这里是获取 UIImage 的代码,根据触摸之间的距离沿着路径(不是真正的 pathRef,只是由点创建的逻辑路径)在屏幕上绘制它,然后旋转图像正确地基于当前点和先前点的 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 像素 x 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;
}

这将创建一个看起来像这样的图像:

enter image description here

将添加以下内容(对上述代码进行一些更改,以尝试填充快速移动时 touchesMoved 缺少一些点的空白:

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: %f y :%f", x, y);
}

关于ios - 在 iOS 中绘制沿路径均匀分布的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6951142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com