iOS: Rotating with CGContextRotate

Im trying to rotate an image, that i want to draw on a image file (to the context. Everything works fine, except when i rotate the image.

Basically i have an image; i want to scale and resize the image and then clip it to a rect and finaly draw it to the UICurrentContext;

//create a new graphic context
UIGraphicsBeginImageContext(CGSizeMake(500, 500));
CGContextRef graphicContext = UIGraphicsGetCurrentContext();
CGContextDrawImage(graphicContext, CGRectMake(0, 0, rect.size.width, rect.size.height), image.CGImage);
CGContextRotateCTM(graphicContext, 45 * M_PI/180.0);
UIImage* editedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//draw it to the current context
[editedImage drawAtPoint:CGPointMake(0,0)];

The thing is when i rotate the image, i dont have a clue what the new size of the imagecontext would be. Next to that, the image in the edited image is not rotated..

Answers


You need to rotate the context before you draw. I know this seems silly, but to use the analogy of a piece of paper...this is a piece of paper that cannot move. Instead you must move yourself around it and then draw.

This way, your size will never change because anything too big will just simply be cut off.

EDIT Also, this function takes radians, not degrees, so you don't need to convert as you are doing. If you want 45 degrees it will just be PI / 4 (which is stored as the constant M_PI_4 in math.h).


Need Your Help

Does using JFace's Viewers preclude good MVC separation?

java model-view-controller user-interface jface

I'm writing a GUI app that I want to use Swing and SWT. The end-user will specify somehow which should be used (it won't use both at the same time!). SWT is what I prefer generally and I have been

AJAX-based groupchat for webpage like on FB

javascript jquery ajax chat push

Got a question for you ... I'm currently working on a simple groupchat functionality for a webpage. It will only be available for groups so there's no need for the users to specify a nickname or an...