Cannot show image in correct format from camera in landscape mode

I am working on an iPad application in Ios6, there, when we press the right bar button, I give an action as shown below:

-(IBAction)camerabuttonAction:(id)sender { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypeCamera; picker.delegate = self; self.popoverController = [[UIPopoverController alloc] initWithContentViewController:picker]; [self.popoverController presentPopoverFromRect:CGRectMake(50, -250, 500, 300) inView:appDelegate.splitview.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; } 

My problem: when I am in Land scape mode, if I press the button. The camera is displayed in portrait mode (the image appears in reverse to see) after each second use. But, if I shake the iPad, then it displays in LandScape ie, in the right direction.

see below image

When I am in Land scape mode, if I press the camera button, an image will appear as shown below:

enter image description here

If I shake the iPad, the camera displays an image as shown below:

enter image description here

I tried a lot and searched googled, but I did not find any solution. This is killing my time, so if someone has worked on this, please help me and send a sample code.

+10
ios iphone ios-simulator ipad uiimagepickercontroller
Mar 22 '13 at 5:51 on
source share
6 answers

You can try passing the conversion to your imagePickerController

 imagePickerController.view.transform = CGAffineTransformMakeRotation(-M_PI/2); 
+9
Mar 26 '13 at 11:00
source share

I encountered the same problem in my project, I tried below and it works for me

My code is:

 - (UIImage *)scaleAndRotateImage:(UIImage *)image { int kMaxResolution = 640; // Or whatever CGImageRef imgRef = image.CGImage; CGFloat width = CGImageGetWidth(imgRef); CGFloat height = CGImageGetHeight(imgRef); CGAffineTransform transform = CGAffineTransformIdentity; CGRect bounds = CGRectMake(0, 0, width, height); if (width > kMaxResolution || height > kMaxResolution) { CGFloat ratio = width/height; if (ratio > 1) { bounds.size.width = kMaxResolution; bounds.size.height = roundf(bounds.size.width / ratio); } else { bounds.size.height = kMaxResolution; bounds.size.width = roundf(bounds.size.height * ratio); } } CGFloat scaleRatio = bounds.size.width / width; CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); CGFloat boundHeight; UIImageOrientation orient = image.imageOrientation; switch(orient) { case UIImageOrientationUp: //EXIF = 1 transform = CGAffineTransformIdentity; break; case UIImageOrientationUpMirrored: //EXIF = 2 transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0); transform = CGAffineTransformScale(transform, -1.0, 1.0); break; case UIImageOrientationDown: //EXIF = 3 transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height); transform = CGAffineTransformRotate(transform, M_PI); break; case UIImageOrientationDownMirrored: //EXIF = 4 transform = CGAffineTransformMakeTranslation(0.0, imageSize.height); transform = CGAffineTransformScale(transform, 1.0, -1.0); break; case UIImageOrientationLeftMirrored: //EXIF = 5 boundHeight = bounds.size.height; bounds.size.height = bounds.size.width; bounds.size.width = boundHeight; transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width); transform = CGAffineTransformScale(transform, -1.0, 1.0); transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0); break; case UIImageOrientationLeft: //EXIF = 6 boundHeight = bounds.size.height; bounds.size.height = bounds.size.width; bounds.size.width = boundHeight; transform = CGAffineTransformMakeTranslation(0.0, imageSize.width); transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0); break; case UIImageOrientationRightMirrored: //EXIF = 7 boundHeight = bounds.size.height; bounds.size.height = bounds.size.width; bounds.size.width = boundHeight; transform = CGAffineTransformMakeScale(-1.0, 1.0); transform = CGAffineTransformRotate(transform, M_PI / 2.0); break; case UIImageOrientationRight: //EXIF = 8 boundHeight = bounds.size.height; bounds.size.height = bounds.size.width; bounds.size.width = boundHeight; transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0); transform = CGAffineTransformRotate(transform, M_PI / 2.0); break; default: [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"]; } UIGraphicsBeginImageContext(bounds.size); CGContextRef context = UIGraphicsGetCurrentContext(); if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) { CGContextScaleCTM(context, -scaleRatio, scaleRatio); CGContextTranslateCTM(context, -height, 0); } else { CGContextScaleCTM(context, scaleRatio, -scaleRatio); CGContextTranslateCTM(context, 0, -height); } CGContextConcatCTM(context, transform); CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef); UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return imageCopy; } 
+10
Mar 29 '13 at 11:03
source share

I am researching your problem on a Google search and getting some result or opportunity, as below: -

result 1

some Answer say its error is iOS 6, so you cannot fix it as below Quetion: -

IPad camera popover preview of irregular rotation and scale

result 2

I do not think we can control the orientation of the camera. The camera orientation property is built-in, which changes with the device orientation.

IPad Camera Orientation Orientation Mode?

result 3

you can control the scaled live iPhone-camera in the center, physically move the pick-up frame. from Bellow code: -

 [picker.view setFrame:CGRectMake(xOffset,yOffset,picker.view.frame.size.width,picker.view.frame.size.height)]; 

hope you got a solution in fact. about this error or problem

+2
Mar 23 '13 at 7:56
source share

I am going to try a good guess here because I did not do it myself ...

Have you tried to subclass the UIImagePickerController and implement the interfaceOrientation methods from the ViewController (which is a superclass)?

+1
Mar 26 '13 at 10:50
source share

Please, try

 [popController presentPopoverFromRect:CGRectMake(1024, self.view.bounds.origin.y + (self.view.bounds.size.height / 2), 1, 1) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES]; 
+1
Mar 26 '13 at 11:20
source share

I only have lanscape mode for ipad, I was worried because I read all these messages, but I used this very simple code to take a snapshot and it works great for my only lanscape application. It’s important that I don’t select an image from the library, I just take the image and use it to transfer it to another image, it works fine for me.

 if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; imagePicker.mediaTypes = [NSArray arrayWithObjects: (NSString *) kUTTypeImage, nil]; imagePicker.allowsEditing = NO; [self presentViewController:imagePicker animated:YES completion:nil]; // [imagePicker release]; newMedia = YES; } -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [self.popoverController dismissPopoverAnimated:true]; // [popoverController release]; NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; [self dismissViewControllerAnimated:YES completion:nil]; if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) { UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; imageView.image = image; if (newMedia) UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:finishedSavingWithError:contextInfo:), nil); } else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) { // Code here to support video if enabled } } 
+1
Jun 18 '13 at 18:48
source share



All Articles