Http://blog.sina.com.cn/s/blog_6602ffbc0101ckx3.html
The thing to share here is to rotate the image instead of ImageView it, by using quartz2d to draw the picture, and then using CTM to make the rotation.
Note: The left side of the quartz2d drawing is not the same as the left side of the drawing in OC, causing the picture to be reversed. So it's going to make it a positive, and then a further spin.
+ (UIImage *) Image: (UIImage *) image rotation: (uiimageorientation) orientation
{
Long double rotate = 0.0;
CGRect rect;
float TranslateX = 0;
float Translatey = 0;
float ScaleX = 1.0;
float ScaleY = 1.0;
Switch (orientation) {
Case Uiimageorientationleft:
rotate = m_pi_2;
Rect = CGRectMake (0, 0, image.size.height, image.size.width);
TranslateX = 0;
Translatey =-rect.size.width;
ScaleY = Rect.size.width/rect.size.height;
ScaleX = Rect.size.height/rect.size.width;
Break
Case Uiimageorientationright:
Rotate = 3 * m_pi_2;
Rect = CGRectMake (0, 0, image.size.height, image.size.width);
TranslateX =-rect.size.height;
Translatey = 0;
ScaleY = Rect.size.width/rect.size.height;
ScaleX = Rect.size.height/rect.size.width;
Break
Case Uiimageorientationdown:
rotate = M_PI;
Rect = CGRectMake (0, 0, image.size.width, image.size.height);
TranslateX =-rect.size.width;
Translatey =-rect.size.height;
Break
Default
rotate = 0.0;
Rect = CGRectMake (0, 0, image.size.width, image.size.height);
TranslateX = 0;
Translatey = 0;
Break
}
Uigraphicsbeginimagecontext (rect.size);
Cgcontextref context = Uigraphicsgetcurrentcontext ();
Make a CTM transformation
CGCONTEXTTRANSLATECTM (context, 0.0, rect.size.height);
CGCONTEXTSCALECTM (Context, 1.0,-1.0);
CGCONTEXTROTATECTM (context, rotate);
CGCONTEXTTRANSLATECTM (context, TranslateX, Translatey);
CGCONTEXTSCALECTM (context, ScaleX, ScaleY);
Drawing pictures
Cgcontextdrawimage (context, CGRectMake (0, 0, Rect.size.width, rect.size.height), image. Cgimage);
UIImage *newpic = Uigraphicsgetimagefromcurrentimagecontext ();
return newpic;
}
How iOS turns image to 90,180,270 degrees