我试图找出一种添加或添加路径(已经完成)的方法,并且能够以某种方式设置绘制路径的位置。

这是我正在谈论的示例:

//everything here can be compiled in a regular UIViewController
//this example is very trivial but it illustrates basically what I want to do
CGRect preMadeRect = CGRectMake(0.0f, 0.f, 50.f, 50.f);
CGPathRef preMadePath = CGPathCreateWithEllipseInRect(preMadeRect, NULL);

UIGraphicsBeginImageContext(self.view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();

//I thought that maybe if I moved to a new point
//the path would respect the starting point as its new
//x and y coordinates
CGContextMoveToPoint(context, 50.f, 50.f);
//I am adding a path
//I want the path to be positioned at new coordinates
//within the context
CGContextAddPath(context, preMadePath);
CGContextStrokePath(context);

UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageView *imageView = [[UIImageView alloc]initWithImage:resultImage];
[self.view addSubview: imageView];

任何想法,将不胜感激。

最佳答案

您无法更改路径中的点;他们被设置。但是,您可以将变换应用于上下文,而改为更改坐标系。

您的路径定义了一个在原点(0,0)处有一个角的矩形。默认情况下,原点位于图像的左上角(或左下角)。如果将上下文的坐标系平移50,50,然后绘制路径(仍为0,0),则它将出现距角50个像素的位置。

在文档中查找CGContextTranslateCTM

如果您正在做其他绘图并且需要撤消坐标系更改,则可能还需要使用CGContextSaveGStateCGContextRestoreGState

10-08 07:53
查看更多