UIImage+Extensions.swift 6.5 KB

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