IOS development-solves the problem of 90 degrees rotation of photos after taking a photo (with the Swift version included), iosswift
After taking a photo with a camera, call the delegate method of UIImagePickerController, as shown in the following code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];}
You can use the UIImagePickerControllerOriginalImage key to obtain the UIImage after the image is taken from the info dictionary. However, if you view the image at the breakpoint, you will find that the image is rotated 90 degrees.
However, if you use an image directly, for example, if you use this UIImage to assign a value to a UIImageView instance, the displayed image is completely correct.
However, if you use this UIImage for other purposes, such as uploading to the server, a problem may occur, and the image uploaded to the server is rotated 90 degrees.
Cause: You can use the UIImagePickerControllerOriginalImage key to obtain the UIImage after the photo from the info dictionary. The default imageOrientation attribute is UIImageOrientationRight, which means that the image is rotated 90 degrees to the left by default.
The solution is as follows:
Method 1: when creating a UIImagePickerController instance, set the allowsEditing attribute of the instance to YES. After taking the photo, a box is displayed, you can crop and edit a photo. When taking a photo in the proxy method of UIImagePickerController, do not use the UIImagePickerControllerOriginalImage key. Instead, use images. In this case, the imageOrientation of the image is UIImageOrientationUp instead of the default UIImageOrientationRight.
Method 2: There is a good Category dedicated to the problem of UIImage rotation. The Code is as follows:
OC version:
UIImage + fixOrientation. h
#import <UIKit/UIKit.h>@interface UIImage (fixOrientation)- (UIImage *)fixOrientation;@end
UIImage + fixOrientation. m
#import "UIImage+fixOrientation.h"@implementation UIImage (fixOrientation)- (UIImage *)fixOrientation { // No-op if the orientation is already correct if (self.imageOrientation == UIImageOrientationUp) return self; // We need to calculate the proper transformation to make the image upright. // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored. CGAffineTransform transform = CGAffineTransformIdentity; switch (self.imageOrientation) { case UIImageOrientationDown: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height); transform = CGAffineTransformRotate(transform, M_PI); break; case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: transform = CGAffineTransformTranslate(transform, self.size.width, 0); transform = CGAffineTransformRotate(transform, M_PI_2); break; case UIImageOrientationRight: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, 0, self.size.height); transform = CGAffineTransformRotate(transform, -M_PI_2); break; } switch (self.imageOrientation) { case UIImageOrientationUpMirrored: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, self.size.width, 0); transform = CGAffineTransformScale(transform, -1, 1); break; case UIImageOrientationLeftMirrored: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, self.size.height, 0); transform = CGAffineTransformScale(transform, -1, 1); break; } // Now we draw the underlying CGImage into a new context, applying the transform // calculated above. CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height, CGImageGetBitsPerComponent(self.CGImage), 0, CGImageGetColorSpace(self.CGImage), CGImageGetBitmapInfo(self.CGImage)); CGContextConcatCTM(ctx, transform); switch (self.imageOrientation) { case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: case UIImageOrientationRight: case UIImageOrientationRightMirrored: // Grr... CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage); break; default: CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage); break; } // And now we just create a new UIImage from the drawing context CGImageRef cgimg = CGBitmapContextCreateImage(ctx); UIImage *img = [UIImage imageWithCGImage:cgimg]; CGContextRelease(ctx); CGImageRelease(cgimg); return img;}@end
Swift version:
Import UIKitextension UIImage {// fixed image rotation func fixOrientation ()-> UIImage {if self. imageOrientation =. up {return self} var transform = CGAffineTransform. identity switch self. imageOrientation {case. down ,. downMirrored: transform = transform. translatedBy (x: self. size. width, y: self. size. height) transform = transform. rotated (:. pi) break case. left ,. leftMirrored: transform = transform. trans LatedBy (x: self. size. width, y: 0) transform = transform. rotated (:. pi/2) break case. right ,. rightMirrored: transform = transform. translatedBy (x: 0, y: self. size. height) transform = transform. rotated (:-. pi/2) break default: break} switch self. imageOrientation {case. upMirrored ,. downMirrored: transform = transform. translatedBy (x: self. size. width, y: 0) transform = transform. scaledBy (x: -1, y: 1) break case. leftMirrored ,. rightMirrored: transform = transform. translatedBy (x: self. size. height, y: 0); transform = transform. scaledBy (x:-1, y: 1) break default: break} let ctx = CGContext (data: nil, width: Int (self. size. width), height: Int (self. size. height), bitsPerComponent: self. cgImage !. BitsPerComponent, bytesPerRow: 0, space: self. cgImage !. ColorSpace !, BitmapInfo: self. cgImage !. BitmapInfo. rawValue) ctx ?. Concatenate (transform) switch self. imageOrientation {case. left,. leftMirrored,. right,. rightMirrored: ctx ?. Draw (self. cgImage !, In: CGRect (x: CGFloat (0), y: CGFloat (0), width: CGFloat (size. height), height: CGFloat (size. width) break default: ctx ?. Draw (self. cgImage !, In: CGRect (x: CGFloat (0), y: CGFloat (0), width: CGFloat (size. width), height: CGFloat (size. height) break} let cgimg: CGImage = (ctx ?. MakeImage ())! Let img = UIImage (cgImage: cgimg) return img }}