views:

1079

answers:

3

I want to draw some texts using Quartz. Now the app draws alt text, but I want it to show as "0123456789". Is there any way to show its normal orientation?

Here is my code:

    //set image view
 drawImage = [[UIImageView alloc] initWithImage:nil];
 drawImage.frame = self.view.frame;
 [self.view addSubview:drawImage];

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

 CGContextSelectFont(context, "Courier", 40,  kCGEncodingMacRoman);
 CGFloat white[] = {1.0, 1.0, 1.0, 1.0};
 CGContextSetFillColor(context, white);
 CGContextShowTextAtPoint(context, 0, 30, "0123456789", strlen("0123456789"));

 // get image
 drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();    // end
A: 

What do you mean, exactly, by "rotate them to the normal mode"?

Assuming you just mean rotation within a 2D space, you would typically use a transform matrix. Have a look at CGAffineTransformMakeRotation.

You could apply it to the view itself (by setting the transform property), or to the CG context you have.

Phil Nash
Ummm...I take a screenshot to make the issue more clear. I want to draw 0123456789 but it turns out to be the image above. I think a single rotation won't work for this issue. I need to mirror the current image and rotate it.
iPhoney
Right, yes I see now. In that case Chintan's reply is accurate.
Phil Nash
+5  A: 

In Quartz, the image will be upside down because its Y-Axis is inverted.

iPhone's Y axis goes positive from top to bottom i.e. origin is at top left while in Quartz and OpenGL, the co-ordinates are the same way as good old geometry i.e. origin at bottom left.

Here is some code for solving Quartz inversion problem:

CGContextRef ctx = UIGraphicsGetCurrentContext();
// Tanslate and scale upside-down to compensate for Quartz's inverted coordinate system
CGContextTranslateCTM(ctx, 0.0, rect.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);

Here is an interesting post about it where the author says a team in Singapore gave up an iPhone project due to this inversion problem which they could not figure out: http://trandangkhoa.blogspot.com/2009/07/iphone-os-drawing-image-and-stupid.html

Chintan Patel
+3  A: 

Another way to handle this would be to replace the Quartz text drawing code (from CGSelectFont() to CGContextShowTextAtPoint ()) with something like

[text drawAtPoint:CGPointMake(0.0f, 30.0f) withFont:[UIFont fontWithName:@"Courier" size:40.0f]];

where text is an NSString. On the iPhone, -drawAtPoint:withFont: automatically flips the text to account for the inverted Y axis of the UIViews.

This approach gives you the advantage of handling a wider character set than the MacRoman encoding in the pure Quartz text drawing can. However, it is slightly slower.

Brad Larson
@Brad Larson, definitely slower, much slower, "measure it and find out" slower? Just wondering: I do like short code when possible.
Yar
@Yar - Noticeably slower, unfortunately. The system appears to use the web rendering core functions to display the text, where the Quartz drawing calls draw the text directly. It's a tradeoff between full Unicode support and fast drawing. There is a way around this by prerendering glyphs on the Mac and providing them with the application for each font and size, but I've not tried this myself.
Brad Larson
@Brad Larson, thanks for that. I'll keep this thread around for optimization.
Yar