4

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.

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
Yama
  • 2,649
  • 3
  • 31
  • 63
  • Is [that][1] what are you looking for? [1]: http://stackoverflow.com/questions/5973105/image-clicked-from-iphone-in-portrait-mode-gets-rotated-by-90-degree – Novarg Jan 10 '12 at 14:55

2 Answers2

7

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.

Community
  • 1
  • 1
Novarg
  • 7,390
  • 3
  • 38
  • 74
0

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.

nielsbot
  • 15,922
  • 4
  • 48
  • 73
  • It does not allow me to set orientation with image.orientation. – Yama Jan 09 '12 at 10:37
  • no you set the orientation on your new `UIImage` by calling `+[UIImage imageWithCGImage:scale:orientation:]` instead of `+[UIImage imageWithCGImage:]`. If you can't do that, you'll have to rotate/flip your `UIImage` before rendering it into your `CGContext` – nielsbot Jan 09 '12 at 10:39
  • yes that is what I am doing. it allows me to set scale but when in orientation field i write image. ,then there is no option like orientation after dot(.) notation – Yama Jan 09 '12 at 10:45
  • The property is called `imageOrientation`. See the [documentation](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImage_Class/Reference/Reference.html) for details. – Jiri Jan 09 '12 at 15:17
  • Are you trying to crop an image taken with the camera? see this answer: http://stackoverflow.com/questions/7203847/how-to-properly-crop-an-image-taken-on-iphone-4g-with-exif-rotation-data – nielsbot Jan 09 '12 at 22:02