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.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;
}
Turn: imageorientation question about camera photos on iphone