7

i load image from camera roll, but this image is upside down. so i wrote method to rotate it.

CGImageRef imageRef = [image CGImage];

float width = CGImageGetWidth(imageRef);
float height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
Byte *rawData = malloc(height * width * 4);
Byte bytesPerPixel = 4;
int bytesPerRow = bytesPerPixel * width;
Byte bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
int byteIndex = 0;
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);

Byte *rawData2 = malloc(height * width * 4);

for (int i = 0 ; i < width * height ; i++) {
    int index = (width * height) * 4;
    rawData2[byteIndex + 0] = rawData[index - byteIndex + 0];
    rawData2[byteIndex + 1] = rawData[index - byteIndex + 1];
    rawData2[byteIndex + 2] = rawData[index - byteIndex + 2];
    rawData2[byteIndex + 3] = rawData[index - byteIndex + 3];

    byteIndex += 4;
}

CGContextRef ctx = CGBitmapContextCreate(rawData2, CGImageGetWidth( imageRef ), CGImageGetHeight( imageRef ), 8, CGImageGetBytesPerRow( imageRef ), CGImageGetColorSpace( imageRef ),kCGImageAlphaPremultipliedLast );

imageRef = CGBitmapContextCreateImage (ctx);
image = [UIImage imageWithCGImage:imageRef];

CGContextRelease(context);

return image;

it's ok, but now i must flip it horizontally, and i don't know how can i do this. i try to do this second day.

thank you for help

Tomasz Szulc
  • 4,217
  • 4
  • 43
  • 79

1 Answers1

23

Have you tried this:

imageView.transform = CGAffineTransformMakeScale(-1, 1);

?

You can also do the rotation by using a transformation:

imageView.transform = CGAffineTransformMakeRotation(M_PI);

You can concat the two transformation in one like this:

imageView.transform = CGAffineTransformRotation(CGAffineTransformMakeScale(-1, 1), M_PI);

If you want to make your own UIImage object, rather than manipulating views and transformations, I would still suggest you to use the approach described above to make the view draw the image as you like, then convert your UIView content into an UIImage object:

UIGraphicsBeginImageContext(rect.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
sergio
  • 68,819
  • 11
  • 102
  • 123
  • i must transform UIImage and draw it on CGContextRef, so `.transform` isn't available. I try to add UIImage to UIImageView, rotate it and make `image = imageView.image`, but it's not working. – Tomasz Szulc Jul 29 '12 at 10:33
  • Please, see my edit as to how you can "convert" the view content into an image. – sergio Jul 29 '12 at 10:35
  • it's working but i set `imageview.frame = CGRectMake(111,122,768,1024)` and it render context at `0,0,768,1024`. any suggestion? – Tomasz Szulc Jul 29 '12 at 11:26
  • ok thank you for help. i add imageView to subView and render it. – Tomasz Szulc Jul 29 '12 at 11:33