Commit fe213425 authored by Zandor Smith's avatar Zandor Smith 💻

Add UIImage fixOrientation function.

parent 3ceacf36
......@@ -83,6 +83,7 @@
F909969F4E3192AE569CED47CC017DA1 /* GeneralToolsFramework-dummy.m in Sources */ = {isa = PBXBuildFile; fileRef = 51B4FD7AAF13BB567C1A730E0D550E3A /* GeneralToolsFramework-dummy.m */; };
FCA382C123550BB600190994 /* UIFont+DynamicType.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCA382BF23550AC600190994 /* UIFont+DynamicType.swift */; };
FCB6F9901B4B6EF0133C1651310B89D8 /* UIRoundedButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5803C2ED326D9E98B7B88E990661836D /* UIRoundedButton.swift */; };
FCB70CB723745EDD0042590B /* UIImage+Orientation.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCB70CB623745EDD0042590B /* UIImage+Orientation.swift */; };
FCE1CF75232EB61900CC7444 /* UIGradientButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = FCE1CF73232EB55A00CC7444 /* UIGradientButton.swift */; };
/* End PBXBuildFile section */
......@@ -341,6 +342,7 @@
FB4883E8C623BAFFAF2A9A8947FFD7C2 /* Pods-GeneralToolsFramework_Example-frameworks.sh */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.script.sh; path = "Pods-GeneralToolsFramework_Example-frameworks.sh"; sourceTree = "<group>"; };
FBDE79DA7B3CA0BACFE69AD27EA16D73 /* AppConfiguration.html */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.html; name = AppConfiguration.html; path = docs/Enums/AppConfiguration.html; sourceTree = "<group>"; };
FCA382BF23550AC600190994 /* UIFont+DynamicType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIFont+DynamicType.swift"; sourceTree = "<group>"; };
FCB70CB623745EDD0042590B /* UIImage+Orientation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+Orientation.swift"; sourceTree = "<group>"; };
FCCF655753371DF0FC624F07E1BBB186 /* ConnectivityPercentage.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = ConnectivityPercentage.swift; path = Connectivity/Classes/Model/ConnectivityPercentage.swift; sourceTree = "<group>"; };
FCE1CF73232EB55A00CC7444 /* UIGradientButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIGradientButton.swift; sourceTree = "<group>"; };
FDDC8C67095E105471BCD7F2A125235F /* APICallError.html */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.html; name = APICallError.html; path = docs/Enums/APICallError.html; sourceTree = "<group>"; };
......@@ -664,6 +666,7 @@
029631A7522890CCB99FE35D830F49EB /* UITableViewCell+GetTableView.swift */,
B6B1CDF2C728FC4047B456258E2BEA1E /* UIViewController+Dismiss.swift */,
FCA382BF23550AC600190994 /* UIFont+DynamicType.swift */,
FCB70CB623745EDD0042590B /* UIImage+Orientation.swift */,
);
name = Extensions;
path = GeneralToolsFramework/Classes/Extensions;
......@@ -1047,6 +1050,7 @@
76196851F6E6716C19A73DBD486B4A06 /* Date+Ago.swift in Sources */,
1A97E4DE49009346CA9C2E78C84D7123 /* Date+Dutch.swift in Sources */,
1E314272A838FD3DAC2578E70A3C810F /* Date+MySQL.swift in Sources */,
FCB70CB723745EDD0042590B /* UIImage+Orientation.swift in Sources */,
EAEF8800005028E32D22389510691C9D /* Double+Round.swift in Sources */,
F79D7961AB2DA28A6B9F9A90C71E6F07 /* Float+DutchString.swift in Sources */,
7AC7B1C8A4CC1E28EA2057A8E68DCFD9 /* Float+Round.swift in Sources */,
......
//
// UIImage+Orientation.swift
// GeneralToolsFramework
//
// Created by Zandor Smith on 07/11/2019.
//
import UIKit
extension UIImage {
/// Automatically rotate the UIImage to the orientation in the orientation exif so that that won't have to be done in the backend or on the clients that don't natively support the orientation exif.
func fixOrientation() -> UIImage {
// No-op if the orientation is already correct
if (self.imageOrientation == .up) {
return self
}
// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if left/right/down, and then flip if mirrored.
var transform: CGAffineTransform = .identity
if ( self.imageOrientation == .down || self.imageOrientation == .downMirrored ) {
transform = transform.translatedBy(x: self.size.width, y: self.size.height)
transform = transform.rotated(by: CGFloat(Double.pi))
}
if ( self.imageOrientation == .left || self.imageOrientation == .leftMirrored ) {
transform = transform.translatedBy(x: self.size.width, y: 0)
transform = transform.rotated(by: CGFloat(Double.pi / 2))
}
if ( self.imageOrientation == .right || self.imageOrientation == .rightMirrored ) {
transform = transform.translatedBy(x: 0, y: self.size.height);
transform = transform.rotated(by: CGFloat(-Double.pi / 2));
}
if (self.imageOrientation == .upMirrored || self.imageOrientation == .downMirrored) {
transform = transform.translatedBy(x: self.size.width, y: 0)
transform = transform.scaledBy(x: -1, y: 1)
}
if ( self.imageOrientation == .leftMirrored || self.imageOrientation == .rightMirrored ) {
transform = transform.translatedBy(x: self.size.height, y: 0)
transform = transform.scaledBy(x: -1, y: 1)
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
let cgImage = self.cgImage!
let ctx: CGContext = CGContext(data: nil, width: Int(self.size.width), height: Int(self.size.height), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: cgImage.colorSpace!, bitmapInfo: cgImage.bitmapInfo.rawValue)!
ctx.concatenate(transform)
if (self.imageOrientation == .left || self.imageOrientation == .leftMirrored
|| self.imageOrientation == .right || self.imageOrientation == .rightMirrored) {
ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: self.size.height, height: self.size.width))
} else {
ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
}
// And now we just create a new UIImage from the drawing context and return it
return UIImage(cgImage: ctx.makeImage()!)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment