IPhone: image captured in portrait mode loaded in landscape mode in UIImageview

I am developing an application in which I take an image in portrait mode and then switch to another class with image data and display that image in UIImageView on UIScrollView.

Now, if I take the image form library, it works fine, but if I need to capture an image, it will display the data in landscape mode. Below is my code for both classes .:

Grade 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]; } 

Grade 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 don’t understand where I am making a mistake? Can anyone point me to? Thanks in advance.

+4
source share
2 answers

Just found a terrific answer to your question. Here's the link: fooobar.com/questions/21815 / ...

And then you can do something like this:

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

and the image will be your image with the correct orientation. He worked in my project.

Hope this helps.

+7
source

be careful not to throw out orientation data ... Try the following:

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

you must also make sure that you crop the correct pixels depending on the orientation of the image.

0
source

Source: https://habr.com/ru/post/1390016/


All Articles