Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS PNG Image rotated 90 degrees

In my iOS application I'm writing, I deal with PNGs because I deal with the alpha channel. For some reason, I can load a PNG into my imageView just fine, but when it comes time to either copy the image out of my application (onto the PasteBoard) or save the image to my camera roll, the image rotates 90 degrees.

I've searched everywhere on this, and one of the things I learned is that if I used JPEGs, I wouldn't have this problem (it sounds), due to the EXIF information.

My app has full copy/paste functionality, and here's the kicker (I'll write this in steps so it is easier to follow):

  1. Go to my camera roll and copy an image
  2. Go into my app and press "Paste", image pastes just fine, and I can do that all day
  3. Click the copy function I implemented, and then click "Paste", and the image pastes but is rotated.

I am 100% sure my copy and paste code isn't what is wrong here, because if I go back to Step 2 above, and click "save", the photo saves to my library but it is rotated 90 degrees!

What is even more strange is that it seems to work fine with images downloaded from the internet, but is very hit or miss with images I manually took with the phone. Some it works, some it doesn't...

Does anybody have any thoughts on this? Any possible work arounds I can use? I'm pretty confident in the code being it works for about 75% of my images. I can post the code upon request though.

like image 351
Boeckm Avatar asked Apr 24 '12 23:04

Boeckm


People also ask

How do I rotate a picture 90 degrees on my iPhone?

Tap Edit in the upper right corner. Tap the rotate/crop icon at the bottom of the screen. Select the rotate icon at left. Each time this button is touched, the image will rotate 90 degrees.

Why does my picture keep uploading sideways?

The reason your photo would appear this way is because the photo was taken that way (either with the phone sideways or upside down) and the image file itself is in this orientation. For example, if you hold your phone upright and take a photo, the photo is saved in portrait mode or "sideways".


1 Answers

For those that want a Swift solution, create an extension of UIImage and add the following method:

func correctlyOrientedImage() -> UIImage {     if self.imageOrientation == .up {         return self     }      UIGraphicsBeginImageContextWithOptions(size, false, scale)     draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))     let normalizedImage = UIGraphicsGetImageFromCurrentImageContext();     UIGraphicsEndImageContext();      return normalizedImage ?? self; } 
like image 136
Tom J Avatar answered Sep 20 '22 03:09

Tom J