There is no need to set the orientationMode
for the destination image. The orientationMode
is an indication how to rotate the image when drawing it. That is, the UIImage
contains the original, unrotated, data (usually in CGImage
format), and the orientationMode
property. When drawing the UIImage
(for example when adding it to a UIImageView
), it will rotate it as well, as needed.
Your code is also drawing the image, and therefore will rotate it:
[sourceImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
The destImage
will contain the rotated original data, and orientationMode
UIImageOrientationUp
. The net result is, that drawing the destImage
will result in the same image with the same orientation as the sourceImage
.
The only difference comes when you actually use the CGImage
original data from each (and therefore ignoring the orientationMode
property), then the destination image will be a rotated version of the source. If you are going to work with the CGImage
anyways, then it's better to use the CGImage
level operations to resize the image, for example (this comes from http://fingertwister.tumblr.com/post/9074775714/code-to-create-a-resized-cgimage-from-a-cgimage, I did not verify it):
+ (CGImageRef)resizeCGImage:(CGImageRef)image toWidth:(int)width andHeight:(int)height {
// create context, keeping original image properties
CGColorSpaceRef colorspace = CGImageGetColorSpace(image);
CGContextRef context = CGBitmapContextCreate(NULL, width, height,
CGImageGetBitsPerComponent(image),
CGImageGetBytesPerRow(image),
colorspace,
CGImageGetAlphaInfo(image));
CGColorSpaceRelease(colorspace);
if(context == NULL)
return nil;
// draw image to context (resizing it)
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
// extract resulting image from context
CGImageRef imgRef = CGBitmapContextCreateImage(context);
CGContextRelease(context);
return imgRef;
}