Sarah Sarah - 5 months ago 28
Objective-C Question

iPhone : Image Captured in portrait mode is loaded in landscape mode in UIImageview

I am developing an Application where I am capturing image in portrait mode and then moving to another class with Image data and showing that image in a UIImageView on a UIScrollView.

Now If I am taking image form library,it is working fine, but If I have to capture an Image it is showing data in landscape mode. Below is my code of both classes. :

Class 1:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSData *imgData = UIImagePNGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"]);

[picker dismissModalViewControllerAnimated:YES];
image = [[UIImage alloc] initWithData:imgData];

CGImageRef imgRefCrop = image.CGImage;
UIImage *photo = [UIImage imageWithCGImage:imgRefCrop];

PhotoCropperPage *photoCropper = [[PhotoCropperPage alloc]
initWithPhoto:photo
delegate:self
uiMode:PCPUIModePresentedAsModalViewController
showsInfoButton:YES];

[photoCropper setMinZoomScale:0.3f];
[photoCropper setMaxZoomScale:4.0f];
[self.navigationController pushViewController:photoCropper animated:YES];
}


Class 2:

- (void) loadPhoto
{
if (self.photo == nil) {
return;
}

CGFloat w = self.photo.size.width;
CGFloat h = self.photo.size.height;

CGRect imageViewFrame = CGRectMake(0.0f, 0.0f, roundf(w / 2.0f), roundf(h / 2.0f));
self.scrollView.contentSize = imageViewFrame.size;

UIImageView *iv = [[UIImageView alloc] initWithFrame:imageViewFrame];
iv.image = self.photo;
[self.scrollView addSubview:iv];
self.imageView = iv;
[iv release];
}


I am not getting where I am doing a mistake? Can anybody point me out? Thanks in advance.

Answer

Just found an awesome answer to your question. Here's the link: http://stackoverflow.com/a/5427890/1047258

And then you can do something like that:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  UIImage *theImage = [[info objectForKey:@"UIImagePickerControllerOriginalImage"]fixOrientation];
  //rest of your code
}

and theImage will be your image with correct orientation. It worked in my project

Hope it helps.