Occasionally, when shooting a photo from an album or picture camera black solves the problem by solving the problem and turning the head in a way

Source: Internet
Author: User

I tried a lot of methods online. No solution!

Its purpose is the same, it is based on the direction of the image orientation for the rotation of the corresponding angle to achieve the effect we want.!

The bar code to watch directly, I was imagepickercontroller to get the picture

#pragma mark-uiimagepickercontrollerdelegate methods//When you select a picture to enter here-(void) Imagepickercontroller: ( Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (nsdictionary *) info{    //Photo processing here  //  NSString *type = [info objectforkey:uiimagepickercontrollermediatype];//*    UIImage *originalimage = [INFO] Objectforkey:uiimagepickercontrolleroriginalimage];    Uiimageorientation orientation = originalimage.imageorientation;    if (orientation = = Uiimageorientationup) {        [self dismissviewcontrolleranimated:yes completion:nil];        _membericonview.image = Originalimage;    } else if (orientation = = uiimageorientationright) {        [self dismissviewcontrolleranimated:yes completion:nil];        _membericonview.image = [Originalimage imagerotatedbydegrees:90.0];    }}

Here I would like to say that some people on the http://stackoverflow.com/point out that this method can solve the problem, but my problem still exists!

The methods on StackOverflow are as follows:

-(UIImage *) fixorientation{if (self.imageorientation = = Uiimageorientationup) {return self;    } Cgaffinetransform Transform;    NSLog (@ "%i", self.imageorientation);            Switch (self.imageorientation) {case Uiimageorientationdown:case uiimageorientationdownmirrored: {            Transform = Cgaffinetransformtranslate (transform, Self.size.width, self.size.height);            Transform = Cgaffinetransformrotate (transform, M_PI);        Break } case Uiimageorientationleft:case uiimageorientationleftmirrored: {transform = Cgaffine            Transformtranslate (transform, self.size.width, 0);            Transform = Cgaffinetransformrotate (transform, m_pi_2);        Break } case uiimageorientationright: {transform = cgaffinetransformtranslate (transform, 0, Self.size.            height);            Transform = Cgaffinetransformrotate (transform,-m_pi_2);        Break } Case UiimaGeorientationrightmirrored: {transform = cgaffinetransformtranslate (transform, 0, self.size.height);            Transform = Cgaffinetransformrotate (transform,-m_pi_2);        Break    } Default:break; } switch (self.imageorientation) {case Uiimageorientationupmirrored:case uiimageorientationdownmirrored            : Transform = cgaffinetransformtranslate (transform, self.size.width, 0);            Transform = Cgaffinetransformscale (transform,-1, 1);                    Break Case Uiimageorientationleftmirrored:case Uiimageorientationrightmirrored:transform = CGAffineTransform            Translate (transform, self.size.height, 0);            Transform = Cgaffinetransformscale (transform,-1, 1);    Break                                             } cgcontextref CTX = Cgbitmapcontextcreate (NULL, Self.size.width, Self.size.height, Cgimagegetbitspercomponent (self.          Cgimage), 0,                                   Cgimagegetcolorspace (self. Cgimage), Cgimagegetbitmapinfo (self.    Cgimage));    CGCONTEXTCONCATCTM (CTX, transform); Switch (self.imageorientation) {case Uiimageorientationleft:case UIIMAGEORIENTATIONLEFTMIRRORED:CA            Se uiimageorientationright:case uiimageorientationrightmirrored://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 context cgimageref cgimg = Cgbitmapcontextcreateimage    (CTX);    UIImage *img = [UIImage imagewithcgimage:cgimg];    Cgcontextrelease (CTX);    Cgimagerelease (CGIMG); return img;}

Finally, I synthesized other methods. One up to fill the UIImage class extension, to finally solve this problem:

Method: In fact, he also rotates in the direction of the angle, but he works.

-(UIImage *) Imagerotatedbyradians: (cgfloat) radians {return [self imagerotatedbydegrees:radianstodegrees (radians)];} -(UIImage *) Imagerotatedbydegrees: (cgfloat) degrees {//calculate the size of the rotated view ' s containing box for OU R drawing Space UIView *rotatedviewbox = [[UIView alloc] Initwithframe:cgrectmake (0,0,self.size.width, Self.size.height    )];    Cgaffinetransform t = cgaffinetransformmakerotation (Degreestoradians (degrees));    Rotatedviewbox.transform = t;    Cgsize rotatedsize = rotatedViewBox.frame.size;    Create the bitmap Context uigraphicsbeginimagecontext (rotatedsize);    Cgcontextref bitmap = Uigraphicsgetcurrentcontext ();    Move The origin to the middle of the image so we'll rotate and scale around the center.    CGCONTEXTTRANSLATECTM (Bitmap, ROTATEDSIZE.WIDTH/2, ROTATEDSIZE.HEIGHT/2);    Rotate The image Context Cgcontextrotatectm (bitmap, Degreestoradians (degrees)); Now, draw the rotated/scaled image into the context CgcontextSCALECTM (Bitmap, 1.0,-1.0); Cgcontextdrawimage (Bitmap, CGRectMake (-SELF.SIZE.WIDTH/2,-SELF.SIZE.HEIGHT/2, Self.size.width, Self.size.height),    [Self cgimage]);    UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext ();    Uigraphicsendimagecontext (); return newimage;}

Finally, we enclose all the source code of this class extension, and paste the copy directly to use:

First header file: Uiimage+fixorientation.h

#import <UIKit/UIKit.h> @interface UIImage (fixorientation)-(UIImage *) fixorientation;-(UIImage *) Imagerotatedbyradians: (cgfloat) radians;-(UIImage *) Imagerotatedbydegrees: (cgfloat) degrees; @end

Come again. m File: uiimage+fixorientation.m

#import "Uiimage+fixorientation.h" CGFloat Degreestoradians (CGFloat degrees) {return degrees * m_pi/180;}; CGFloat radianstodegrees (CGFloat radians) {return radians * 180/M_PI;}; @implementation UIImage (fixorientation)-(UIImage *) fixorientation{if (self.imageorientation = = Uiimageorientationup    ) {return self;    } Cgaffinetransform Transform;    NSLog (@ "%i", self.imageorientation);            Switch (self.imageorientation) {case Uiimageorientationdown:case uiimageorientationdownmirrored: {            Transform = Cgaffinetransformtranslate (transform, Self.size.width, self.size.height);            Transform = Cgaffinetransformrotate (transform, M_PI);        Break } case Uiimageorientationleft:case uiimageorientationleftmirrored: {transform = Cgaffine            Transformtranslate (transform, self.size.width, 0);            Transform = Cgaffinetransformrotate (transform, m_pi_2);        Break } Case UiimageorieNtationright: {transform = cgaffinetransformtranslate (transform, 0, self.size.height);            Transform = Cgaffinetransformrotate (transform,-m_pi_2);        Break } case uiimageorientationrightmirrored: {transform = cgaffinetransformtranslate (transform, 0, SE            Lf.size.height);            Transform = Cgaffinetransformrotate (transform,-m_pi_2);        Break    } Default:break; } switch (self.imageorientation) {case Uiimageorientationupmirrored:case uiimageorientationdownmirrored            : Transform = cgaffinetransformtranslate (transform, self.size.width, 0);            Transform = Cgaffinetransformscale (transform,-1, 1);                    Break Case Uiimageorientationleftmirrored:case Uiimageorientationrightmirrored:transform = CGAffineTransform            Translate (transform, self.size.height, 0); Transform = Cgaffinetransformscale (transform,-1, 1);    Break                                             } cgcontextref CTX = Cgbitmapcontextcreate (NULL, Self.size.width, Self.size.height, Cgimagegetbitspercomponent (self. Cgimage), 0, cgimagegetcolorspace (self. Cgimage), Cgimagegetbitmapinfo (self.    Cgimage));    CGCONTEXTCONCATCTM (CTX, transform); Switch (self.imageorientation) {case Uiimageorientationleft:case UIIMAGEORIENTATIONLEFTMIRRORED:CA            Se uiimageorientationright:case uiimageorientationrightmirrored://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 context cgimageref cgimg = CGBITMAPCOntextcreateimage (CTX);    UIImage *img = [UIImage imagewithcgimage:cgimg];    Cgcontextrelease (CTX);    Cgimagerelease (CGIMG); return img;} -(UIImage *) Imagerotatedbyradians: (cgfloat) radians {return [self imagerotatedbydegrees:radianstodegrees (radians)];} -(UIImage *) Imagerotatedbydegrees: (cgfloat) degrees {//calculate the size of the rotated view ' s containing box for OU R drawing Space UIView *rotatedviewbox = [[UIView alloc] Initwithframe:cgrectmake (0,0,self.size.width, Self.size.height    )];    Cgaffinetransform t = cgaffinetransformmakerotation (Degreestoradians (degrees));    Rotatedviewbox.transform = t;    Cgsize rotatedsize = rotatedViewBox.frame.size;    Create the bitmap Context uigraphicsbeginimagecontext (rotatedsize);    Cgcontextref bitmap = Uigraphicsgetcurrentcontext ();    Move The origin to the middle of the image so we'll rotate and scale around the center.    CGCONTEXTTRANSLATECTM (Bitmap, ROTATEDSIZE.WIDTH/2, ROTATEDSIZE.HEIGHT/2); Rotate the Image Context Cgcontextrotatectm (bitmap, Degreestoradians (degrees));    Now, draw the rotated/scaled image into the context Cgcontextscalectm (bitmap, 1.0,-1.0); Cgcontextdrawimage (Bitmap, CGRectMake (-SELF.SIZE.WIDTH/2,-SELF.SIZE.HEIGHT/2, Self.size.width, Self.size.height),    [Self cgimage]);    UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext ();    Uigraphicsendimagecontext (); return newimage;} @end


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Occasionally, when shooting a photo from an album or picture camera black solves the problem by solving the problem and turning the head in a way

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.