---
title: "Fix image orientation issue in RubyMotion"
description:
  "Sometimes images appear in different orientation. This blog discusses how to
  fix that issue in RubyMotion apps."
canonical_url: "https://www.bigbinary.com/blog/rubymotion-image-orientation"
markdown_url: "https://www.bigbinary.com/blog/rubymotion-image-orientation.md"
---

# Fix image orientation issue in RubyMotion

Sometimes images appear in different orientation. This blog discusses how to fix
that issue in RubyMotion apps.

- Author: Neeraj Singh
- Published: August 4, 2013
- Categories: Ruby

I'm building an app using RubyMotion. When I take picture then it all looks
good. However when the picture is posted on web then the orientation of the
picture is different.

## UIImage and UIImageOrientation

[UIImage](https://developer.apple.com/library/ios/documentation/uikit/reference/UIImage_Class/Reference/Reference.html)
in iOS has a property called UIImageOrientation. Image orientation affects the
way the image data is displayed when drawn. The api docs mention that by
default, images are displayed in the `up` orientation. However, if the image has
associated metadata (such as EXIF information), then this property contains the
orientation indicated by that metadata.

After using
[UIImagePickerController](https://developer.apple.com/library/ios/documentation/uikit/reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html)
to take an image using the iPhone camera, I was using BubbleWrap to send the
image to a webserver. When the image is taken in landscape/portrait mode, then
the image appeared fine when it is viewed in the browser. But, when the image is
sent back via api and is shown on the iphone, the image is rotated by 90 degrees
if the image is taken in portrait mode. In exif metadata, iOS incorrectly sets
the orientation to UIImageOrientationRight .

Here is how I fixed the image orientation issue:

```ruby
if image.imageOrientation == UIImageOrientationUp
  return_image = image
else
  UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
  image.drawInRect([[0,0], image.size])
  normalized_image = UIImage.UIGraphicsGetImageFromCurrentImageContext
  UIGraphicsEndImageContext()
  return_image = normalized_image
end
```

First, we are checking the image orientation of the image we have in hand. If
the image orientation is UIImageOrientationUp, we don't have to change anything.
Otherwise we are redrawing the image and returning the normalized image.

## Links

- [Human page](https://www.bigbinary.com/blog/rubymotion-image-orientation)
