Losing image orientation while converting an image to CGImage Losing image orientation while converting an image to CGImage ios ios

Losing image orientation while converting an image to CGImage


SWIFT 3:

convert rotated cgImage to UIImage by this method

UIImage(cgImage:croppedCGImage, scale:originalImage.scale, orientation:originalImage.imageOrientation)

Source @David Berry answer


Here's a UIImage extension I wrote after looking after looking at several older pieces of code written by others. It's written in Swift 3 and uses the iOS orientation property plus CGAffineTransform to re-draw the image in proper orientation.

SWIFT 3:

public extension UIImage {    /// Extension to fix orientation of an UIImage without EXIF    func fixOrientation() -> UIImage {        guard let cgImage = cgImage else { return self }        if imageOrientation == .up { return self }        var transform = CGAffineTransform.identity        switch imageOrientation {        case .down, .downMirrored:            transform = transform.translatedBy(x: size.width, y: size.height)            transform = transform.rotated(by: CGFloat(M_PI))        case .left, .leftMirrored:            transform = transform.translatedBy(x: size.width, y: 0)            transform = transform.rotated(by: CGFloat(M_PI_2))        case .right, .rightMirrored:            transform = transform.translatedBy(x: 0, y: size.height)            transform = transform.rotated(by: CGFloat(-M_PI_2))        case .up, .upMirrored:            break        }        switch imageOrientation {        case .upMirrored, .downMirrored:            transform.translatedBy(x: size.width, y: 0)            transform.scaledBy(x: -1, y: 1)        case .leftMirrored, .rightMirrored:            transform.translatedBy(x: size.height, y: 0)            transform.scaledBy(x: -1, y: 1)        case .up, .down, .left, .right:            break        }        if let ctx = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: cgImage.colorSpace!, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) {            ctx.concatenate(transform)            switch imageOrientation {            case .left, .leftMirrored, .right, .rightMirrored:                ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.height, height: size.width))            default:                ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))            }            if let finalImage = ctx.makeImage() {                return (UIImage(cgImage: finalImage))            }        }        // something failed -- return original        return self    }}


I found a solution.... time will tell if it's robust enough, but it seems to work in all situations. That was a vicious bug to fix.

So the problem is that UIImage, in some case only, lose it's orientation when converted to CGImage. It affects portraits image, that are automatically put in landscape mode. So image that are landscape by default are not affected.But where the bug is vicious is that it doesn't affect ALL portrait images !! Also imageorientation value won't help for some image.Those problematic images are images that user has in it's library that he got from email, messages, or saved from the web, so not taken with a camera. These images possibly don't have orientation information, and thus in some case, an image in portrait.... REMAINS in portrait when converted to CGImage. I really got stuck on that until I realized that some of my image in my device library were saved from messages or emails.

So the only reliable way I found to guess which image will be reoriented, is to create both version of a given image: UIImage, and CGImage, and compare their height value. If they are equal, then the CGImage version will not be rotated and you could work with it as expected.But if they height are different, you can be sure that the CGImage conversion from CGImageCreateWithImageInRect will landscapize the image.In this case only, I swap the x/y coordinate of origin, that I pass as rectangle coordinate to crop and it treats those special images correctly.

That was a long post, but the main idea is to compare CGImage height to UIImage width, and if they are different, expect origin point to be inverted.