I modified your example and found that simply drawing a UIImage into a graphics context and then creating a fresh UIImage removes the orientation by default. The following code does a scale with aspect ratio preservation and orientation correction.
// Draw the image into the transformed context and return the image
[image drawAtPoint:CGPointMake(0.0f, 0.0f)];
UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newimg;
}
If you see a flaw with this approach, let me know.
I modified your example and found that simply drawing a UIImage into a graphics context and then creating a fresh UIImage removes the orientation by default. The following code does a scale with aspect ratio preservation and orientation correction.
(UIImage ) scaleImage: (UIImage )image scaleFactor:(float)scaleBy { CGSize size = CGSizeMake(image.size.width * scaleBy, image.size.height * scaleBy);
UIGraphicsBeginImageContext(size); CGContextRef context = UIGraphicsGetCurrentContext(); CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformScale(transform, scaleBy, scaleBy); CGContextConcatCTM(context, transform);
// Draw the image into the transformed context and return the image [image drawAtPoint:CGPointMake(0.0f, 0.0f)]; UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
return newimg;
}
If you see a flaw with this approach, let me know.
Thanks for all the great recipes! Jason