MFMailComposeViewController image orientation

后端 未结 6 1117
攒了一身酷
攒了一身酷 2020-11-28 07:57

I\'m developing a Universal app and I\'m coding for iOS6.

I\'m using the imagePickerController to take a photo and then I am sending it as an attachment using MFMail

6条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 08:26

    I think you can get the UIImage API to handle the rotation for you automatically, without needing to manually do the transforms.

    The documentation for the UIImage methods size and drawInRect: both say they take the orientation into account, so I drawInRect the UIImage into a new context and grab the resultant (auto-rotated) image from there. Here's the code in a category:

    @interface UIImage (Orientation)
    
    - (UIImage*)imageAdjustedForOrientation;
    
    @end
    
    @implementation UIImage (Orientation)
    - (UIImage*)imageAdjustedForOrientation
    {
        // The UIImage methods size and drawInRect take into account 
        //  the value of its imageOrientation property
        //  so the rendered image is rotated as necessary.
        UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
    
        [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
    
        UIImage *orientedImage = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        return orientedImage;
    }
    @end
    

    I just found this better answer by an0 which is virtually the same, but also includes the optimisation of not re-redrawing the image if the orientation is already correct.

提交回复
热议问题