Yama
Yama

Reputation: 2649

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.

Upvotes: 4

Views: 6194

Answers (2)

Novarg
Novarg

Reputation: 7450

Just found an awesome answer to your question. Here's the link: https://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.

Upvotes: 7

nielsbot
nielsbot

Reputation: 16031

be careful not to throw away the orientation data... Try this:

image = [[UIImage alloc] initWithData:imgData];

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

also, you'll want to make sure you're cropping the right pixels depending in the image's orientation.

Upvotes: 0

Related Questions