Fix photo Orientation

Source: Internet
Author: User


Fix photo Orientation
+ (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 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 (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;
}

Fix photo Orientation

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.