Fix the picture orientation that iOS gets from cameras and albums

Source: Internet
Author: User

Fix the picture orientation that iOS gets from cameras and albums

The default direction of images taken with the system camera is sometimes not imageorientationdown, but Imageorientationleft, which appears when used in a clockwise deflection of 90 °. Use the Fixorientation method to fix the problem.

- (UIImage *) fixorientation{No-op If the orientation is already correctif (Self.imageorientation = =Uiimageorientationup)ReturnSelfWe 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 (Self.imageorientation) {CaseUiimageorientationdown:CaseUiimageorientationdownmirrored:transform =Cgaffinetransformtranslate (Transform,Self.size.width,Self.size.height); Transform =Cgaffinetransformrotate (transform, M_PI);BreakCaseUiimageorientationleft:CaseUiimageorientationleftmirrored:transform =Cgaffinetransformtranslate (Transform,Self.size.width,0); Transform =Cgaffinetransformrotate (transform, m_pi_2);BreakCaseUiimageorientationright:CaseUiimageorientationrightmirrored:transform =Cgaffinetransformtranslate (Transform,0,Self.size.height); Transform =Cgaffinetransformrotate (transform,-m_pi_2);BreakDefaultBreak }Switch (Self.imageorientation) {CaseUiimageorientationupmirrored:CaseUiimageorientationdownmirrored:transform =Cgaffinetransformtranslate (Transform,Self.size.width,0); Transform =Cgaffinetransformscale (Transform,-1,1);BreakCaseUiimageorientationleftmirrored:CaseUiimageorientationrightmirrored:transform =Cgaffinetransformtranslate (Transform,Self.size.height,0); Transform =Cgaffinetransformscale (Transform,-1,1);BreakDefaultBreak }Now we draw the underlying cgimage into a new context, applying the transformCalculated above.Cgcontextref CTX =Cgbitmapcontextcreate (NullSelf.size.width,Self.size.height,Cgimagegetbitspercomponent (Self. Cgimage),0,Cgimagegetcolorspace (Self. Cgimage),Cgimagegetbitmapinfo (Self. Cgimage));CGCONTEXTCONCATCTM (CTX, transform);Switch (Self.imageorientation) {CaseUiimageorientationleft:CaseUiimageorientationleftmirrored:CaseUiimageorientationright:CaseUiimageorientationrightmirrored: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 CG Imageref cgimg = cgbitmapcontextcreateimage (CTX); uiimage *img = [uiimage Imagewith cgimage:cgimg]; cgcontextrelease (CTX); cgimagerelease (cgimg); return img;}            

Fix the orientation of the pictures that iOS gets from the camera and album

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.