How iOS turns image to 90,180,270 degrees

Source: Internet
Author: User

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

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.