In different frameworks of iOS, different coordinate systems are used:
uikit:y shaft Down/ Core Graphics (Quartz): Y-Axis up/OpenGL es:y axis up
Uikit is the core framework of the iphone SDK's cocoa touch layer, the iphone application graphical interface and event-driven foundation, which, like traditional Windows desktops, has a y-axis downward;
The Core Graphics (Quartz) is a 2D-based graphics drawing engine whose coordinate system is y-axis upward, while OpenGL ES is the 2D and 3D drawing engine of the iphone SDK, which uses the left-handed coordinate system, which sits
The scale is also the y-axis upward, and if the z-axis is not considered, its coordinate system and quartz are the same in two dimensions.
In addition, the pictures imported from the camera need to be processed in the direction first! Photos taken with the camera contain EXIF information, UIImage's imageorientation attribute refers to the orientation information in EXIF. Such as
We ignore the orientation information, and directly to the photo pixel processing or drawinrect, the result is flipped or rotated 90 after the appearance. This is because we perform pixel processing or
After the drawinrect operation, the imageorientaion information is deleted, Imageorientaion is reset to 0, resulting in the photo content and imageorientaion mismatch. So, before the photos are processed,
The photo is rotated in the correct direction first, and the Imageorientaion returned is 0.
List the methods in 3:
1. Determine the direction of the picture and rotate the picture
1-(UIImage *) Fixorientation: (UIImage *) Aimage {2 3 //No-op If the orientation is already correct4 if(Aimage.imageorientation = =uiimageorientationup)5 returnAimage;6 7Cgaffinetransform transform =cgaffinetransformidentity;8 9 Switch(aimage.imageorientation) {Ten CaseUiimageorientationdown: One Caseuiimageorientationdownmirrored: ATransform =cgaffinetransformtranslate (Transform, AImage.size.width, aImage.size.height); -Transform =cgaffinetransformrotate (transform, M_PI); - Break; the - CaseUiimageorientationleft: - Caseuiimageorientationleftmirrored: -Transform = Cgaffinetransformtranslate (transform, AImage.size.width,0); +Transform =cgaffinetransformrotate (transform, m_pi_2); - Break; + A CaseUiimageorientationright: at Caseuiimageorientationrightmirrored: -Transform = Cgaffinetransformtranslate (transform,0, aImage.size.height); -Transform = Cgaffinetransformrotate (transform,-m_pi_2); - Break; - default: - Break; in } - to Switch(aimage.imageorientation) { + Caseuiimageorientationupmirrored: - Caseuiimageorientationdownmirrored: theTransform = Cgaffinetransformtranslate (transform, AImage.size.width,0); *Transform = Cgaffinetransformscale (transform,-1,1); $ Break;Panax Notoginseng - Caseuiimageorientationleftmirrored: the Caseuiimageorientationrightmirrored: +Transform = Cgaffinetransformtranslate (transform, AImage.size.height,0); ATransform = Cgaffinetransformscale (transform,-1,1); the Break; + default: - Break; $ } $ - //Now we draw the underlying cgimage into a new context, applying the transform - //calculated above. theCgcontextref CTX =cgbitmapcontextcreate (NULL, AImage.size.width, AImage.size.height, -Cgimagegetbitspercomponent (Aimage.cgimage),0,Wuyi Cgimagegetcolorspace (aimage.cgimage), the Cgimagegetbitmapinfo (Aimage.cgimage)); - CGCONTEXTCONCATCTM (CTX, transform); Wu Switch(aimage.imageorientation) { - CaseUiimageorientationleft: About Caseuiimageorientationleftmirrored: $ CaseUiimageorientationright: - Caseuiimageorientationrightmirrored: - //Grr ... -Cgcontextdrawimage (CTX, CGRectMake (0,0, Aimage.size.height,aimage.size.width), aimage.cgimage); A Break; + the default: -Cgcontextdrawimage (CTX, CGRectMake (0,0, Aimage.size.width,aimage.size.height), aimage.cgimage); $ Break; the } the the //And now we just create a new UIImage from the drawing context theCgimageref cgimg =cgbitmapcontextcreateimage (CTX); -UIImage *img =[UIImage imagewithcgimage:cgimg]; in cgcontextrelease (CTX); the cgimagerelease (cgimg); the returnimg; About}
2. use UIImage's Drawinrect function, which automatically handles the correct orientation of the picture
-(UIImage *) Normalizedimage: (UIImage *) noimage { ifreturn noimage; Uigraphicsbeginimagecontextwithoptions (Noimage.size, NO, Noimage.scale); [Noimage drawinrect: (CGRect) {00, noimage.size}]; *normalizedimage = uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); return normalizedimage;}
3. Drawing pictures using Cgcontext
0 , height); 1.0,-1.0); Cgcontextdrawimage (Context, CGRectMake (00, width, height), uiimage.cgimage);
Handle picture Upside down