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..
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).