Iphone: image taken from a change in camera orientation

I made an iphone application to capture a camera image and set this image in the following view. But the problem is that the image rotates. Thus, the landscape image becomes beautiful, and the protraite image becomes landscape. I referenced many codes, but could not find a solution.

My code is:

- (void)btnCapturePressed { if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { picker=[[UIImagePickerController alloc] init]; picker.delegate=self; [picker setAllowsEditing:YES]; picker.sourceType=UIImagePickerControllerSourceTypeCamera; //[self.navigationController pushViewController:(UIViewController *)ipc animated:YES]; [self presentModalViewController:picker animated:YES]; [picker release]; } } -(void) imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo :(NSDictionary *)info { UIImage *imageToScale=[info objectForKey:UIImagePickerControllerOriginalImage]; imgView = [[UIImageView alloc] initWithImage:imageToScale]; [picker presentModalViewController:cropper animated:YES]; } 

I also referred to the link. with the same problem, but could not find a solution.

Please help me.

Thank.

+2
objective-c iphone ios5
Jul 02 2018-12-12T00:
source share
4 answers

Thus, in order to preserve the orientation of the device during the image and pass it to the method below as a parameter. Here, simply give any name to the method and pass the orientation parameter

 switch (orientation) { case UIDeviceOrientationPortrait: [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(0.))]; break; case UIDeviceOrientationPortraitUpsideDown: [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(180.))]; break; case UIDeviceOrientationLandscapeLeft: [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(90.))]; break; case UIDeviceOrientationLandscapeRight: [featureLayer setAffineTransform:CGAffineTransformMakeRotation(DegreesToRadians(-90.))]; break; case UIDeviceOrientationFaceUp: case UIDeviceOrientationFaceDown: default: break; // leave the layer in its last known orientation } 

and the macro that I used here DegreesToRadians() as follows

 static CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}; 

This will definitely work.

Happy coding :)

EDIT

If the code above does not work, use this

 @interface UIImage (RotationMethods) - (UIImage *)imageRotatedByDegrees:(CGFloat)degrees; @end @implementation UIImage (RotationMethods) - (UIImage *)imageRotatedByDegrees:(CGFloat)degrees { // calculate the size of the rotated view containing box for our drawing space UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)]; CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees)); rotatedViewBox.transform = t; CGSize rotatedSize = rotatedViewBox.frame.size; // Create the bitmap context UIGraphicsBeginImageContext(rotatedSize); CGContextRef bitmap = UIGraphicsGetCurrentContext(); // Move the origin to the middle of the image so we will rotate and scale around the center. CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); // // Rotate the image context CGContextRotateCTM(bitmap, DegreesToRadians(degrees)); // Now, draw the rotated/scaled image into the context CGContextScaleCTM(bitmap, 1.0, -1.0); CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } @end 

and then call the above function below

 switch (orientation) { case UIDeviceOrientationPortrait: image = [image imageRotatedByDegrees:0]; break; case UIDeviceOrientationPortraitUpsideDown: image = [image imageRotatedByDegrees:180]; break; case UIDeviceOrientationLandscapeLeft: image = [image imageRotatedByDegrees:-90]; break; case UIDeviceOrientationLandscapeRight: image = [image imageRotatedByDegrees:90]; break; case UIDeviceOrientationFaceUp: case UIDeviceOrientationFaceDown: default: break; // leave the layer in its last known orientation } 

If the image is not in the desired orientation, add 90 to all of the above imageRotatedByDegrees arguments (i.e. if it is 0, then it is 0 + 90) or as needed.

EDIT 1

 UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation]; 
+4
Jul 03 '12 at 7:38
source share

I struggled with this problem, but found a much easier solution:

  AVCaptureConnection *vc = [self.snapper connectionWithMediaType:AVMediaTypeVideo]; // THIS IS THE KEY LINE!!!!!!!!!!!!!!!!!!!!! [vc setVideoOrientation:[self interfaceToVideoOrientation]]; [self.snapper captureStillImageAsynchronouslyFromConnection:vc completionHandler: ^(CMSampleBufferRef buf, NSError *err) { NSData* data = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:buf]; UIImage* im = [UIImage imageWithData:data]; // Do something with the image. }]; - (AVCaptureVideoOrientation)interfaceToVideoOrientation { switch ([UIApplication sharedApplication].statusBarOrientation) { case UIInterfaceOrientationPortrait: return AVCaptureVideoOrientationPortrait; case UIInterfaceOrientationPortraitUpsideDown: return AVCaptureVideoOrientationPortraitUpsideDown; case UIInterfaceOrientationLandscapeRight: return AVCaptureVideoOrientationLandscapeRight; case UIInterfaceOrientationLandscapeLeft: return AVCaptureVideoOrientationLandscapeLeft; default: return AVCaptureVideoOrientationPortrait; } } 

Adjusting the orientation of the video before the capture fixed my problem when the image comes out with the wrong orientation.

+2
Mar 24 '15 at 6:19 06:19
source share

I am posting a discussion to the apple forum ( https://discussions.apple.com/thread/1537011?start=0&tstart=0 ) and found the following solution; It worked great for me; I just copy it, and it worked like a gem;

 -(UIImage *) scaleAndRotateImage(UIImage *)image { int kMaxResolution = 320; // 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 = bounds.size.width / ratio; } else { bounds.size.height = kMaxResolution; bounds.size.width = 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; } 
+1
Jan 03 '13 at 10:00
source share

I had the same problem and it helped me. Hope this helps too. It took me 2 minutes to fix this problem. http://xcodetipss.blogspot.com/2012/04/image-rotated-when-taken-from-camera.html

+1
Jun 02 '14 at 9:37
source share



All Articles