While UIImage is very convenient to work with, it’s short on — okay, entirely absent of — methods to manipulate the underlying image. In general applying a transform to a view suffices for display purposes, but if you want to change the underlying image, you’re out of luck. Unless, that is, you continue reading; for today we’d like to point you to a convenient code snippet , scaleAndRotateImage:
The problems:
- UIImage provides no easy way to scale, rotate or transform the underlying CGImage. While it is easy to transform it by placing the image in a view, if you need the actual image changed then good luck.
- UIImageJPEGRepresentation(), the easiest (only?) way to convert a UIImage to a JPEG uses the underlying CGImage, and ignores your UIImage imageOrientation, so regardless of camera position when the picture was taken the exported JPEG will always be oriented in landscape or right mode, ending up with pictures that need rotation.
The solution: Thanks to some help in the Apple Support forums, and the #iphonedev IRC chat groups, I was able to clean up a function that takes a UIImage, and fixes it’s underlying core image.
So hey, if you have a reason to scale, rotate, or mirror a UIImage … there you go!
JAN