UIImage+Extensions.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 UIKit
  24. import Accelerate
  25. extension UIImage {
  26. @objc func resizeImage(size: CGSize, isAspectRation: Bool) -> UIImage? {
  27. let originRatio = self.size.width / self.size.height
  28. let newRatio = size.width / size.height
  29. var newSize = size
  30. if isAspectRation {
  31. if originRatio < newRatio {
  32. newSize.height = size.height
  33. newSize.width = size.height * originRatio
  34. } else {
  35. newSize.width = size.width;
  36. newSize.height = size.width / originRatio
  37. }
  38. }
  39. UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
  40. self.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
  41. let newImage = UIGraphicsGetImageFromCurrentImageContext()
  42. UIGraphicsEndImageContext()
  43. if let image = newImage {
  44. return image
  45. }
  46. return self
  47. }
  48. func fixedOrientation() -> UIImage? {
  49. guard imageOrientation != UIImage.Orientation.up else {
  50. // This is default orientation, don't need to do anything
  51. return self.copy() as? UIImage
  52. }
  53. guard let cgImage = self.cgImage else {
  54. // CGImage is not available
  55. return nil
  56. }
  57. 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 {
  58. return nil // Not able to create CGContext
  59. }
  60. var transform: CGAffineTransform = CGAffineTransform.identity
  61. switch imageOrientation {
  62. case .down, .downMirrored:
  63. transform = transform.translatedBy(x: size.width, y: size.height)
  64. transform = transform.rotated(by: CGFloat.pi)
  65. case .left, .leftMirrored:
  66. transform = transform.translatedBy(x: size.width, y: 0)
  67. transform = transform.rotated(by: CGFloat.pi / 2.0)
  68. case .right, .rightMirrored:
  69. transform = transform.translatedBy(x: 0, y: size.height)
  70. transform = transform.rotated(by: CGFloat.pi / -2.0)
  71. case .up, .upMirrored:
  72. break
  73. @unknown default:
  74. break
  75. }
  76. // Flip image one more time if needed to, this is to prevent flipped image
  77. switch imageOrientation {
  78. case .upMirrored, .downMirrored:
  79. transform = transform.translatedBy(x: size.width, y: 0)
  80. transform = transform.scaledBy(x: -1, y: 1)
  81. case .leftMirrored, .rightMirrored:
  82. transform = transform.translatedBy(x: size.height, y: 0)
  83. transform = transform.scaledBy(x: -1, y: 1)
  84. case .up, .down, .left, .right:
  85. break
  86. @unknown default:
  87. break
  88. }
  89. ctx.concatenate(transform)
  90. switch imageOrientation {
  91. case .left, .leftMirrored, .right, .rightMirrored:
  92. ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.height, height: size.width))
  93. default:
  94. ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
  95. break
  96. }
  97. guard let newCGImage = ctx.makeImage() else { return nil }
  98. return UIImage.init(cgImage: newCGImage, scale: 1, orientation: .up)
  99. }
  100. @objc func image(color: UIColor, size: CGFloat) -> UIImage {
  101. let size = CGSize(width: size, height: size)
  102. UIGraphicsBeginImageContextWithOptions(size, false, self.scale)
  103. color.setFill()
  104. let context = UIGraphicsGetCurrentContext()
  105. context?.translateBy(x: 0, y: size.height)
  106. context?.scaleBy(x: 1.0, y: -1.0)
  107. context?.setBlendMode(CGBlendMode.normal)
  108. let rect = CGRect(origin: .zero, size: size)
  109. guard let cgImage = self.cgImage else { return self }
  110. context?.clip(to: rect, mask: cgImage)
  111. context?.fill(rect)
  112. let newImage = UIGraphicsGetImageFromCurrentImageContext() ?? self
  113. UIGraphicsEndImageContext()
  114. return newImage
  115. }
  116. func imageColor(_ color: UIColor) -> UIImage {
  117. if #available(iOS 13.0, *) {
  118. return self.withTintColor(color, renderingMode: .alwaysOriginal)
  119. } else {
  120. return UIGraphicsImageRenderer(size: size, format: imageRendererFormat).image { _ in
  121. color.set()
  122. withRenderingMode(.alwaysTemplate).draw(at: .zero)
  123. }
  124. }
  125. }
  126. }