iosxcodeuiimagelandscape-portraituiimageorientation

transforming UIImage to LandscapeRight in iOS


In my app, I am adding different images from the library into an array and then they are converted into the slide show. I am able to do this, but the only problem I am facing is the orientation of different images. All I want is, that when a user selects a picture from library, before inserting into the array, its orientation should be checked, and if it is other than landscape right, it should be rotated to landscape right. How can I do that? Moreover, the rotated images should be aspect fill i.e. there should be no black margins at the sides of the rotated images.

P.S. I want to rotate UIImage, not UIImageView as there are some ways available to rotate UIImageView but they don't meet my requirements.


Solution

  • You can simply rotate image like this:

    UIImage *newImage = [[UIImage alloc] initWithCGImage: myImage.CGImage
                                                     scale: 1.0
                                               orientation: UIImageOrientationRight];
    

    This should give you a pointer, than you can decide whether to rotate the image or not by comparing its size for e-g:

    if (myImage.size.width < myImage.size.height) {
       // its portrait, do the rotation
    }