Under the Bridge

Snippet: UIImage alterations

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!

4
  • http://www.dyot.biz Dominic

    Is this THE Alex Curylo formerly of Sardis and St Mary’s?

  • http://www.alexcurylo.com/ Alex

    Yes. Yes, it would be.Far as I know, there is only the one.

    And would this be THE Dominic Shadbolt formerly of Abbotsford and St. Mary’s, perchance?

  • http://timuralhimenkov.ru Timur I.

    Wow! Thank you!
    I always wanted to write in my blog something like that. Can I take part of your post to my site?
    Of course, I will add backlink?

    Regards, Timur Alhimenkov

  • Albert Einstein’s ghost

    And when the aliens came to Earth they realized that the secret to space and time travel was randomly hidden in some unrelated blog. Anti-matter and black holes intrinsically have negative spacetime curvature. Spacetime for anti-matter is hyperbolic in nature. While the speed of light is still a constant throughout all inertial reference frames the amount of time it takes to travel through anti-matter is less than through matter.