How do I sync Quartz drawing-path coordinates with the physical iPhone UIView coordinates?
How do I sync Quartz drawing-path coordinates with the physical iPhone UIView coordinates?
- Subject: How do I sync Quartz drawing-path coordinates with the physical iPhone UIView coordinates?
- From: "Frederick C. Lee" <email@hidden>
- Date: Mon, 22 Jun 2009 11:05:07 -0700
Greetings:
Here's my (common) problem: I want to be able to use Quartz to
draw an open path across an CGImage layout based on the GGImage
coordinates.
I use the UITouch object/click via Simulator to collect my screen
coordinates and hence, to plot a path for Quartz to draw:
CGPoint myPoint = [touch locationInView:self];
However, these coordinates aren't the same as the Quartz path
coordinates I need to actually draw a path:
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:@"dodgerStadium2.png"];
CGFloat idealSize = 300.0f;
CGFloat ratio = 1.0f;
CGFloat heightRatio = idealSize / image.size.height;
CGFloat widthRatio = idealSize / image.size.width;
if(heightRatio < widthRatio) {
ratio = heightRatio;
} else {
ratio = widthRatio;
}
// use -10 as the y origin because we are flipping the coordinate
system
CGRect imageRect = CGRectMake(10.0f, -10.0f, image.size.width *
ratio,
image.size.height * ratio);
CGMutablePathRef path = CGPathCreateMutable();
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextScaleCTM(ctx, 2.0f, -2.0f); // (context, x, y).
// move down the height of the image
CGContextTranslateCTM(ctx, -50.0f, -(image.size.height * ratio));
CGContextDrawImage(ctx, imageRect, image.CGImage);
// -------------------------------------------------------
// --- Prepping the Path parameters for Quartz to draw:
CGContextSetLineWidth(ctx, 1.0f);
CGContextBeginPath(ctx);
// Note: Drawing coordinates (0,260) is upper left corner,
(0,-200.0) lower left Corder.
// (320.0, 260.0) upper Right Corner
CGPathMoveToPoint(path, NULL, 0.00, -200.0f); // Via UITouch:
x=0.000000, y=459.000000
CGPathAddLineToPoint(path, NULL, 159.000000, 143.0f); // Via
UITouch x=159.000000, y=146.000000
CGContextAddPath(ctx, path);
CGContextDrawPath(ctx, kCGPathStroke);
CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);
}
Of course, the Quartz/UITouch coordinate sync changes with the scaling.
So...
How can I chart an accurate plot using the iPhone Simulator/Cursor
upon an image for the Quartz drawing engine to follow?
Regards,
Ric.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden