I have a UIImage that appears to be oriented the correct way when my iPad is held in portrait, but when I get the CGImageRef associated with it, the CGImageRef is rotated 90 degrees counterclockwise. After some Googling, I've learned that this is because the CGImageRef has no orientation data unlike the UIImage. I need to view and modify some of the pixels in the CGImageRef, and I'm currently doing this by directly accessing the rawData variable (image is a UIImage*):
CGImageRef imageRef = [image CGImage];
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); //maybe make ...Gray();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpaceRef, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
However, in order for me to properly modify the pixel data stored in rawData, I need the CGImageRef to be in the correct orientation. How can I rotate the CGImageRef 90 degrees clockwise and then access rawData (the pixel information)?