Created
May 17, 2014 05:22
-
-
Save JatWaston/5e317a59459c34289067 to your computer and use it in GitHub Desktop.
rotate image with degrees
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees image:(UIImage*)image | |
{ | |
// calculate the size of the rotated view's containing box for our drawing space | |
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)]; | |
CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees)); | |
rotatedViewBox.transform = t; | |
CGSize rotatedSize = rotatedViewBox.frame.size; | |
[rotatedViewBox release]; | |
// Create the bitmap context | |
UIGraphicsBeginImageContext(rotatedSize); | |
CGContextRef bitmap = UIGraphicsGetCurrentContext(); | |
// Move the origin to the middle of the image so we will rotate and scale around the center. | |
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); | |
// // Rotate the image context | |
CGContextRotateCTM(bitmap, DegreesToRadians(degrees)); | |
// Now, draw the rotated/scaled image into the context | |
CGContextScaleCTM(bitmap, 1.0, -1.0); | |
CGContextDrawImage(bitmap, CGRectMake(-image.size.width / 2, -image.size.height / 2, image.size.width, image.size.height), [image CGImage]); | |
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return newImage; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment