Skip to content

Instantly share code, notes, and snippets.

@yyjim
Forked from josephsieh/CGImage Rotation.png
Created June 20, 2018 11:13
Show Gist options
  • Save yyjim/4dc3413f835f5f4b58b0d4d075fc05f6 to your computer and use it in GitHub Desktop.
Save yyjim/4dc3413f835f5f4b58b0d4d075fc05f6 to your computer and use it in GitHub Desktop.
Rotate CGImage 90 degree clock wise
-(void) rotate90Degree:(CGImageRef) cgImageRef
{
int cgImageWidth = (int)CGImageGetWidth(cgImageRef);
int cgImageHeight = (int)CGImageGetHeight(cgImageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSUInteger bytesPerPixel = 4;
int targetWidth = cgImageHeight;
int targetHeight = cgImageWidth;
NSUInteger bytesPerRow = bytesPerPixel * targetWidth;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(nil, targetWidth, targetHeight,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);
CGContextRotateCTM (context, -M_PI_2);
CGContextTranslateCTM(context, -(int)targetHeight, 0);
CGContextDrawImage(context, CGRectMake(0, 0, cgImageWidth, cgImageHeight), cgImageRef);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment