1

When Image data is returned and applied to a UIImage, if the data comes from the camera then the image appears rotated 90 degrees.

I tried adding the

extension UIImage {
func correctlyOrientedImage() -> UIImage {
    if self.imageOrientation == UIImageOrientation.up {
        return self
    }

    UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
    self.draw(in: CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width:  self.size.width, height: self.size.height))
)
     let normalizedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext();

    return normalizedImage;
}
}

in my code, I check to see if I have data saved for a specific user and if so I load the image data into profile_image, a UIImageView.

    //profile_image
    if let profile_imager = UserDefaults.standard.object(forKey: String(UserID)) as? Data {
       let data = UserDefaults.standard.object(forKey: String(UserID)) as? Data
        print("profile_imager: \(profile_imager)")
        profile_image.image = UIImage(data: data!)
        profile_image.backgroundColor = .white
    }

How would I go about to use correctlyOrientedImage correctly

Thank you

Johny D Good
  • 427
  • 1
  • 8
  • 26
  • If you take a selfie image, it will rotated 90 degree. So if you want it as it looks while taking the image, then you have to rotate it reserve. – Amir Khan Sep 17 '17 at 11:31
  • @AmirKhan I understand, but i need to know if it indeed rotated, because image could come from gallery or the web (Facebook) – Johny D Good Sep 17 '17 at 11:44

1 Answers1

0

Swift 4.2,

Created an UIImage extension to keep the image in the original position,

extension UIImage {
    func fixedOrientation() -> UIImage {

        if imageOrientation == .up {
            return self
        }

        var transform: CGAffineTransform = CGAffineTransform.identity

        switch imageOrientation {
        case .down, .downMirrored:
            transform = transform.translatedBy(x: size.width, y: size.height)
            transform = transform.rotated(by: CGFloat.pi)
        case .left, .leftMirrored:
            transform = transform.translatedBy(x: size.width, y: 0)
            transform = transform.rotated(by: CGFloat.pi / 2)
        case .right, .rightMirrored:
            transform = transform.translatedBy(x: 0, y: size.height)
            transform = transform.rotated(by: CGFloat.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 cgImage = self.cgImage, let colorSpace = cgImage.colorSpace,
            let ctx: CGContext = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: 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 ctxImage: CGImage = ctx.makeImage() {
                return UIImage(cgImage: ctxImage)
            } else {
                return self
            }
        } else {
            return self
        }
    }
}

Then set profile_image from fixedOrientation() method,

//profile_image
if let profile_imager = UserDefaults.standard.object(forKey: String(UserID)) as? Data {
   let data = UserDefaults.standard.object(forKey: String(UserID)) as? Data
    print("profile_imager: \(profile_imager)")
    let actualImage = UIImage(data: data!)?.fixedOrientation()
    profile_image.image = actualImage
    profile_image.backgroundColor = .white
}
Bappaditya
  • 9,494
  • 3
  • 20
  • 29