After taking a photo and uploading it to iOS, the web Client displays the rotating Swift + OC solution, iosswift + oc
Problem description:
A strange phenomenon occurs when you upload a mobile phone portrait, that is, when you upload a photo, the mobile phone displays the portrait normally, but there is a left-hand 90 degrees problem on the web side.
This problem occurs only when the picture is vertical. The horizontal image does not exist.
Cause analysis:
When taking a photo on the mobile phone, the photo taken with the camera contains EXIF information. When we get the UIImage, we can view the imageOrientation attribute of this Image, which actually refers to the orientation information in EXIF.
If we ignore the orientation information and directly perform pixel processing or uploading operations on the photo, the result will be flipped or rotated after 90.
This is because after pixel processing or drawInRect operations, the imageOrientaion information is deleted, and imageOrientaion is reset to 0, resulting in mismatch between the photo content and imageOrientaion.
Therefore, before processing the image, first rotate it to the correct direction to ensure that the returned imageOrientaion is 0.
Solution:
Swift:
/** Solve the problem of rotating the web display of portrait photos: images larger than 2 MB will be automatically rotated 90 degrees-parameter aImage: <# aImage description #>-returns: <# return value description #> */class func fixOrientation (aImage: UIImage)-> UIImage {if aImage. imageOrientation = UIImageOrientation. up {return aImage} var transform = CGAffineTransformIdentity switch (aImage. imageOrientation) {case. down ,. downMirrored: transform = CGAffineTransformTranslate (transform, aImage. Size. width, aImage. size. height) transform = CGAffineTransformRotate (transform, CGFloat (M_PI) break; case. left ,. leftMirrored: transform = CGAffineTransformTranslate (transform, aImage. size. width, 0) transform = CGAffineTransformRotate (transform, CGFloat (M_PI_2) break; case. right ,. rightMirrored: transform = CGAffineTransformTranslate (transform, 0, aImage. size. height) transform = CGAffineTransfor MRotate (transform, CGFloat (-M_PI_2) break; default: break;} switch (aImage. imageOrientation) {case. upMirrored ,. downMirrored: transform = CGAffineTransformTranslate (transform, aImage. size. width, 0) transform = CGAffineTransformScale (transform,-1, 1) break; case. leftMirrored ,. rightMirrored: transform = CGAffineTransformTranslate (transform, aImage. size. height, 0) transform = CGAffineTransformSc Ale (transform,-1, 1) break; default: break;} let ctx: CGContextRef = CGBitmapContextCreate (nil, Int (aImage. size. width), Int (aImage. size. height), CGImageGetBitsPerComponent (aImage. CGImage), 0, CGImageGetColorSpace (aImage. CGImage), 1 )! CGContextConcatCTM (ctx, transform) switch (aImage. imageOrientation) {case. left ,. leftMirrored ,. right ,. rightMirrored: CGContextDrawImage (ctx, CGRectMake (0, 0, aImage. size. height, aImage. size. width), aImage. CGImage) break; default: CGContextDrawImage (ctx, CGRectMake (0, 0, aImage. size. width, aImage. size. height), aImage. CGImage); break;} let cgimg: CGImageRef = CGBitmapContextCreateImage (ctx )! Let img: UIImage = UIImage (CGImage: cgimg) return img ;}
OC version:
- (UIImage *)fixOrientation:(UIImage *)aImage { // No-op if the orientation is already correct if (aImage.imageOrientation == UIImageOrientationUp) return aImage; // 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 (aImage.imageOrientation) { case UIImageOrientationDown: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height); transform = CGAffineTransformRotate(transform, M_PI); break; case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: transform = CGAffineTransformTranslate(transform, aImage.size.width, 0); transform = CGAffineTransformRotate(transform, M_PI_2); break; case UIImageOrientationRight: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, 0, aImage.size.height); transform = CGAffineTransformRotate(transform, -M_PI_2); break; default: break; } switch (aImage.imageOrientation) { case UIImageOrientationUpMirrored: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, aImage.size.width, 0); transform = CGAffineTransformScale(transform, -1, 1); break; case UIImageOrientationLeftMirrored: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, aImage.size.height, 0); transform = CGAffineTransformScale(transform, -1, 1); break; default: break; } // Now we draw the underlying CGImage into a new context, applying the transform // calculated above. CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height, CGImageGetBitsPerComponent(aImage.CGImage), 0, CGImageGetColorSpace(aImage.CGImage), CGImageGetBitmapInfo(aImage.CGImage)); CGContextConcatCTM(ctx, transform); switch (aImage.imageOrientation) { case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: case UIImageOrientationRight: case UIImageOrientationRightMirrored: // Grr... CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage); break; default: CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.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; }
In the test, after obtaining the image from the photo proxy, you can use this method to process the image and then upload it to the server to solve the problem.
// If the image var img: UIImage = info [UIImagePickerControllerOriginalImage]! UIImage // Save the album watermark (img, self, nil, nil) img = PublicMethod. fixOrientation (img) // compress the image let imgData: NSData = UIImageJPEGRepresentation (img, 0.2 )! // Upload Server self. uploadHeadImg (imgData, headImage: img)
References: http://blog.csdn.net/hitwhylz/article/details/39518463
If you have any questions, please leave a message.