Charith Nidarsha
Charith Nidarsha

Reputation: 4245

CGAffineTransform rotation

I have already drawn a bar chart using core graphics. (Figure 1)

enter image description here

And I need another bar graph as follows. I don't need to write another graph for this and I need to reuse the code I have already written. I guess there's a way to do this using rotation. But the trick is width and height are swapped in 2 charts. Any ideas?

enter image description here

Upvotes: 1

Views: 7651

Answers (1)

Mick MacCallum
Mick MacCallum

Reputation: 130183

If your output is a UIImage and you are displaying it using a simple UIImageView, you could adjust the image view's transform property like so:

#define degreesToRadians(x) (M_PI * x / 180.0)
[myImageView setTransform:CGAffineTransformMakeRotation(degreesToRadians(90))];

Doing this would properly adjust the height and width of the UIImageView. For example if the image view starts out 100x200 after this rotation its frame will be 200x100 and the image within it will rotate as well.

Then you could even adjust the anchor point to rotate from using:

#import <QuartzCore/QuartzCore.h>
[[myImageView layer] setAnchorPoint:CGPointMake(0.5, 0.5)];

The CGPoint (0.5, 0.5) refers to the center of the image view.

Upvotes: 5

Related Questions