1

I'm trying to show an image taken through the camera in Portrait mode but I always get it shown on my UIImageView in Landascape mode. The image is scaled before being added to the UIImageView but it seems this is not the problem as I tried many different solutions found on the web (even some quite smart ones like the one coming from Trevor's Bike Shed).

Here is my code:

UIImage *image = [UIImage imageWithContentsOfFile:imgPath];
CGRect newFrame = [scrollView frame];
UIImage *resizedImage = [ImageViewController imageFromImage:image scaledToSize:newFrame.size];
imageView = [[UIImageView alloc] initWithFrame:newFrame];
[imageView setImage:resizedImage];  
[scrollView setContentSize:imageView.frame.size];    
[scrollView addSubview:imageView];  
[imageView release];

imgPath is the path of the image coming as a parameter and scrollView is an IBOutlet linked to a UIScrollView.

Is there something I'm missing about the UIImageView? As I wrote above, it seems that the problem is not related to the scaling...

stack-o-frankie
  • 457
  • 5
  • 15
  • I too faced this issue. This link might help you. http://stackoverflow.com/questions/1315251/how-to-rotate-a-uiimage-90-degrees – Naveen Thunga Sep 28 '11 at 05:23

3 Answers3

0
UIImage* image=[UIImage imageNamed:@"abc.jpg"];
UIImageOrientation orientation=image.imageOrientation;

Use image in imageView or where ever you want to use,

UIImageView* imageView=[[UIImageView alloc] initWithImage:image];

After that reset the image to orientation it was before usage

UIImage* image1=[UIImage imageWithCGImage:[image CGImage] scale:1.0 orientation:orientation];

image1 is your image in the orientation it was before

NaXir
  • 2,373
  • 2
  • 24
  • 31
0

There is an imageOrientation property for a UIImage. Check out the docs. It can be set through the initializer:

UIImage *rotatedImage = [UIImage imageWithCGImage:[resizedImage CGImage] scale:1 orientation:UIImageOrientationUp]

Creates and returns an image object with the specified scale and orientation factors.

+ (UIImage *)imageWithCGImage:(CGImageRef)imageRef scale:(CGFloat)scale orientation:(UIImageOrientation)orientation 

Parameters:

  • imageRef The Quartz image object.

  • scale The scale factor to use when interpreting the image data. Specifying a scale factor of 1.0 results in an image whose size matches the pixel-based dimensions of the image. Applying a different scale factor changes the size of the image as reported by the size (page 12) property.

  • orientation The orientation of the image data. You can use this parameter to specify any rotation factors applied to the image.

  • Return Value A new image object for the specified Quartz image, or nil if the method could not initialize the image from the specified image reference.

chown
  • 51,908
  • 16
  • 134
  • 170
0

Are you certain that imageOrientation is set as you expect on resizedImage? Incorrect scaling can absolutely mess up imageOrientation.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • One of the methods I've been trying refers to the imageOrientation of the original image and it seems that this is UIImageOrientationUp (0) which is exactly what I would expect. – stack-o-frankie Sep 21 '11 at 14:02
  • Is `UIImageOrientationUp` the same as the original image? Try not scaling; just put the original image in the view. Does it work correctly? Open the original image in Preview on the desktop and make sure it is correct. Save the scaled image to disk (simulator) and open it in Preview. See which piece has the problem. – Rob Napier Sep 21 '11 at 14:45
  • The orientation for the 2 images is the same. I'm taking the image from the camera, how can I test the program on the simulator? – stack-o-frankie Sep 21 '11 at 15:01
  • You can write the image to your documents directory and sync to iTunes. You can then fetch the image on the desktop. – Rob Napier Sep 21 '11 at 17:41