1
votes

I have an application that is set in landscape mode because of the content it contains. One of the things I want to do is take a picture of a piece of paper. I present the UIImagePickerController locked in portrait mode because it fits the paper size. After the user takes the picture I load that image as a background on a UIWebView. The reason I use a webview is because sometimes I need to load a .pdf there as well. Anyway, I'm setting the background using CSS. Here is the code...

//img is the path to an image.
myHtml = [NSString stringWithFormat:
                    @"<html><head>"
                    "</head>"
                    "<body><img src='%@'></body></html>", img];

[resume loadHTMLString:myHtml baseURL:baseURL]; 

The problem is, the image is displayed in landscape when the app returns to the UIWebView. Everything else is normal as far as text etc. Is there some reason that images are rotated 90 degrees to fit properly or something? I have tried pretty much everything with no luck.

The other thing is that we I retake a picture and reload the webView the old image remains.

2

2 Answers

0
votes

You need to change the orientation of the image from its exif header if there is orientation info available in it. Identifying the picture orientation is the hard part, rotation of imgs can be done easily using css -webkit-transform: rotate(-90deg);

0
votes

It does have to do with the exif data. While using a webkit transform may be ok for use in just the web view once, if you want to use the image later and have it always be the right orientation I'd use the categories given here:

http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

The article does a great job explaining exactly why the rotation occurs and the code does a nice job of 'fixing' the 'problem' so that you can then make use of the image without having to do extra things or worry about whether it'll be displayed correctly (even though the UIImageView takes the orientation into account).

You can resize with this to the same size it originally was, and it should fix the orientation issue.