Direction of Ios_uiimage (imageorientation)

Source: Internet
Author: User

Demo

A. Reproduce the problem:

Photos obtained by the picture, due to the size of the wrong, need to be cut. But some of the pictures were cut out and the direction was messy. Look for some quality before you know. Picture has a directional property! Imageorientation.

Two. A picture contains two areas of information:

1. The data itself: the color value of each pixel.

2. File header: The basic information of the picture. For example: the width of the picture. Direction information of the picture.

Three: Why is there a picture direction?

In order for the photos to truly reflect the scenes that people saw when they were shooting, many cameras now include a directional sensor that records the direction of the camera as it was taken and saves the information in the photo. There is still no change in the way the photos are stored, but it remains in the camera's coordinate system, but when the camera is looking at these photos, the camera can rotate the photos according to the orientation information in the photo, and then turn to the angle that suits people's viewing.

Four: What is EXIF?

The explanations on Wikipedia are:

The exchangeable image File format is often abbreviated as EXIF (exchangeable image filename), which is specially set for the photo of the digital camera, which can record the property information and shooting data of the digital photo ... Exif can be attached to JPEG, TIFF, riff and other files.

What do you call orientation?

Among the various information that EXIF covers, there is a label called orientation (rotation), which is used to record the orientation of the image, which is the final position of the camera's writing direction information. It defines a total of eight values:

Note: For the above eight directions, the addition of * is not common, because they represent the orientation of the mirror, if you do not do any processing, regardless of the camera shooting at any angle, can not appear mirroring.

What does this table mean? Let's take a look at the first line, with a value of 1 o'clock, and the value of the two column on the right: Row #0 is Top,column #0 was left side, in fact very well understood, it means that the first row of the photo is at the top, and the first column is located on the side, so this picture is naturally taken from the normal angle.

Five: When will cause the picture direction is wrong?

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.

VI: Solution:

The following method is a UIImage category method, which can be used to achieve the above purposes.

Method One: The most intuitive method.

-(UIImage *) fixorientation {//No-op If the orientation is already correct    if(self.imageorientation = = Uiimageorientationup)returnSelf ; //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(self.imageorientation) { CaseUiimageorientationdown: CaseUiimageorientationdownmirrored:transform=cgaffinetransformtranslate (Transform, Self.size.width, self.size.height); Transform=cgaffinetransformrotate (transform, M_PI);  Break;  CaseUiimageorientationleft: CaseUiimageorientationleftmirrored:transform= Cgaffinetransformtranslate (transform, Self.size.width,0); Transform=cgaffinetransformrotate (transform, m_pi_2);  Break;  CaseUiimageorientationright: CaseUiimageorientationrightmirrored:transform= Cgaffinetransformtranslate (Transform,0, Self.size.height); Transform= Cgaffinetransformrotate (Transform,-m_pi_2);  Break;  CaseUiimageorientationup: Caseuiimageorientationupmirrored: Break; }        Switch(self.imageorientation) { Caseuiimageorientationupmirrored: CaseUiimageorientationdownmirrored:transform= Cgaffinetransformtranslate (transform, Self.size.width,0); Transform= Cgaffinetransformscale (Transform,-1,1);  Break;  Caseuiimageorientationleftmirrored: CaseUiimageorientationrightmirrored:transform= Cgaffinetransformtranslate (transform, Self.size.height,0); Transform= Cgaffinetransformscale (Transform,-1,1);  Break;  CaseUiimageorientationup: CaseUiimageorientationdown: CaseUiimageorientationleft: CaseUiimageorientationright: Break; }        //Now we draw the underlying cgimage into a new context, applying the transform//calculated above.Cgcontextref CTX =cgbitmapcontextcreate (NULL, Self.size.width, Self.size.height, Cgimageget Bitspercomponent (self. Cgimage),0, Cgimagegetcolorspace (self. Cgimage), Cgimagegetbitmapinfo (self.    Cgimage));    CGCONTEXTCONCATCTM (CTX, transform); Switch(self.imageorientation) { CaseUiimageorientationleft: Caseuiimageorientationleftmirrored: CaseUiimageorientationright: Caseuiimageorientationrightmirrored://Grr ...Cgcontextdrawimage (CTX, CGRectMake (0,0, Self.size.height,self.size.width), self.            Cgimage);  Break; default: Cgcontextdrawimage (CTX, CGRectMake (0,0, Self.size.width,self.size.height), self.            Cgimage);  Break; }        //And now we just create a new UIImage from the drawing contextCgimageref cgimg =cgbitmapcontextcreateimage (CTX); UIImage*img =[UIImage imagewithcgimage:cgimg];    Cgcontextrelease (CTX);    Cgimagerelease (CGIMG); returnimg;}

Method Two: Here is the use of the Drawinrect method in UIImage, which will draw the image onto the canvas, and has taken into account the direction of the image.

-(UIImage *) normalizedimage {    ifreturn self ;        Uigraphicsbeginimagecontextwithoptions (Self.size, NO, Self.scale);    [Self Drawinrect: (cgrect) {00, self.size}];     *normalizedimage = uigraphicsgetimagefromcurrentimagecontext ();    Uigraphicsendimagecontext ();     return normalizedimage;}

Technical Support: http://www.cocoachina.com/ios/20150605/12021.html

Direction of Ios_uiimage (imageorientation)

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.