Today, when writing a demo, it is found that if you take the image obtained through the camera, direct operation, such as cropping, scaling, the original image will be rotated 90 degrees.
At first I felt baffled and overwhelmed. Later Baidu a bit, found a solution.
PS: In the process of finding, encountered a saying:
Get original photo from IOS Photos//If the picture is larger than 2M, it will automatically rotate 90 degrees, otherwise do not rotate uiimage* originalimg=[dict objectforkey: Uiimagepickercontrolleroriginalimage];
As to whether it is correct, not sure yet. First mark.
The following solution is pro-test feasible. Original: http://www.cnblogs.com/jiangyazhou/archive/2012/03/22/2412343.html
Photos taken with the camera contain EXIF information, UIImage's imageorientation attribute refers to the orientation information in EXIF.
If we ignore the orientation information, and then directly to the photo pixel processing or drawinrect and so on, the result is flipped or rotated 90 after the appearance. This is because after we perform pixel processing or drawinrect operations, the imageorientaion information is deleted, Imageorientaion is reset to 0, causing the photo content and imageorientaion mismatch.
So, before processing the photo, rotate the photo in the correct direction and return the imageorientaion to 0.
The following method is a UIImage category method, which can be used to achieve the above purposes.
-(UIImage *) Fixorientation: (UIImage *) aimage {//No-op if the orientation is already correct if (Aimage.imageo Rientation = = Uiimageorientationup) return aimage; We need to calculate the proper transformation to make the image upright. We do it on 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 (trans form, 0, aImage.size.height); Transform = Cgaffinetransformrotate (transform,-m_pi_2); Break Default:break; } switch (aimage.imageorientation) {case Uiimageorientationupmirrored:case Uiimageorientationdownmi Rrored:transform = Cgaffinetransformtranslate (transform, aImage.size.width, 0); Transform = Cgaffinetransformscale (transform,-1, 1); Break Case Uiimageorientationleftmirrored:case Uiimageorientationrightmirrored:transform = CGAffineTransform Translate (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;}
iOS development-camera (camera) gets to the picture automatically rotated 90 degree solution