Positioning Pre-made CGPath inside a CGContext

I am trying to figure out a way to add or add a path (which I already did) and somehow set the location of the place where the path will be drawn.

Here is an example of what I am saying:

//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];

Any ideas would be appreciated.

+3
source share
1 answer

You cannot change the waypoints; they are installed. However, you can apply the transformation to the context by changing the coordinate system instead.

(0,0). ( ) . 50, 50, ( 0,0), .

CGContextTranslateCTM .

CGContextSaveGState CGContextRestoreGState, , .

+3

All Articles