UIImage+fixedOrientation.swift 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // UIColor+fixedOrientation.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 27/11/2019.
  6. // Copyright © 2019 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. extension UIImage {
  25. /// Fix image orientaton to protrait up
  26. func fixedOrientation() -> UIImage? {
  27. guard imageOrientation != UIImage.Orientation.up else {
  28. // This is default orientation, don't need to do anything
  29. return self.copy() as? UIImage
  30. }
  31. guard let cgImage = self.cgImage else {
  32. // CGImage is not available
  33. return nil
  34. }
  35. guard let colorSpace = cgImage.colorSpace, let ctx = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else {
  36. return nil // Not able to create CGContext
  37. }
  38. var transform: CGAffineTransform = CGAffineTransform.identity
  39. switch imageOrientation {
  40. case .down, .downMirrored:
  41. transform = transform.translatedBy(x: size.width, y: size.height)
  42. transform = transform.rotated(by: CGFloat.pi)
  43. case .left, .leftMirrored:
  44. transform = transform.translatedBy(x: size.width, y: 0)
  45. transform = transform.rotated(by: CGFloat.pi / 2.0)
  46. case .right, .rightMirrored:
  47. transform = transform.translatedBy(x: 0, y: size.height)
  48. transform = transform.rotated(by: CGFloat.pi / -2.0)
  49. case .up, .upMirrored:
  50. break
  51. @unknown default:
  52. break
  53. }
  54. // Flip image one more time if needed to, this is to prevent flipped image
  55. switch imageOrientation {
  56. case .upMirrored, .downMirrored:
  57. transform = transform.translatedBy(x: size.width, y: 0)
  58. transform = transform.scaledBy(x: -1, y: 1)
  59. case .leftMirrored, .rightMirrored:
  60. transform = transform.translatedBy(x: size.height, y: 0)
  61. transform = transform.scaledBy(x: -1, y: 1)
  62. case .up, .down, .left, .right:
  63. break
  64. @unknown default:
  65. break
  66. }
  67. ctx.concatenate(transform)
  68. switch imageOrientation {
  69. case .left, .leftMirrored, .right, .rightMirrored:
  70. ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.height, height: size.width))
  71. default:
  72. ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
  73. break
  74. }
  75. guard let newCGImage = ctx.makeImage() else { return nil }
  76. return UIImage.init(cgImage: newCGImage, scale: 1, orientation: .up)
  77. }
  78. }