UIImage+Extension.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // UIImage+Extension.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2018/11/21.
  6. // Copyright © 2018 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. extension UIImage {
  10. /// EZSE: scales image
  11. public class func scaleTo(image: UIImage, w: CGFloat, h: CGFloat) -> UIImage {
  12. let newSize = CGSize(width: w, height: h)
  13. UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
  14. image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
  15. let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
  16. UIGraphicsEndImageContext()
  17. return newImage
  18. }
  19. func o2_aspectFitRectForSize(_ size: CGSize) -> CGRect {
  20. let targetAspect: CGFloat = size.width / size.height
  21. let sourceAspect: CGFloat = self.size.width / self.size.height
  22. var rect: CGRect = CGRect.zero
  23. if targetAspect > sourceAspect {
  24. rect.size.height = size.height
  25. rect.size.width = ceil(rect.size.height * sourceAspect)
  26. rect.origin.x = ceil((size.width - rect.size.width) * 0.5)
  27. } else {
  28. rect.size.width = size.width
  29. rect.size.height = ceil(rect.size.width / sourceAspect)
  30. rect.origin.y = ceil((size.height - rect.size.height) * 0.5)
  31. }
  32. return rect
  33. }
  34. //base64 转UIImage
  35. public static func base64ToImage(_ source:String,defaultImage:UIImage=UIImage(named: "personDefaultIcon")!) -> UIImage {
  36. if source != ""{
  37. let theImage = UIImage(data: Data(base64Encoded:source,options:NSData.Base64DecodingOptions.ignoreUnknownCharacters)!)
  38. return theImage!
  39. }else{
  40. return defaultImage
  41. }
  42. }
  43. func fixOrientation() -> UIImage {
  44. var image = self
  45. if self.imageOrientation == .up {
  46. return image
  47. }
  48. var transform = CGAffineTransform.identity
  49. switch self.imageOrientation {
  50. case .down, .downMirrored:
  51. transform = transform.translatedBy(x: image.size.width, y: image.size.height)
  52. transform = transform.rotated(by: .pi / 2)
  53. case .left, .leftMirrored:
  54. transform = transform.translatedBy(x: image.size.width, y: 0)
  55. transform = transform.rotated(by: .pi / 2)
  56. case .right, .rightMirrored :
  57. transform = transform.translatedBy(x: 0, y: image.size.height)
  58. transform = transform.rotated(by: -.pi / 2)
  59. default:
  60. break
  61. }
  62. switch self.imageOrientation {
  63. case .upMirrored, .downMirrored:
  64. transform = transform.translatedBy(x: image.size.width,y: 0)
  65. transform = transform.scaledBy(x: -1, y: 1)
  66. case .rightMirrored, .leftMirrored:
  67. transform = transform.translatedBy(x: image.size.height,y: 0)
  68. transform = transform.scaledBy(x: -1, y: 1)
  69. default:
  70. break
  71. }
  72. let ctx = CGContext(data: nil , width: Int(image.size.width), height: Int(image.size.height), bitsPerComponent: image.cgImage!.bitsPerComponent, bytesPerRow: 0, space: image.cgImage!.colorSpace!, bitmapInfo: image.cgImage!.bitmapInfo.rawValue)
  73. ctx!.concatenate(transform)
  74. switch image.imageOrientation {
  75. case .left, .leftMirrored, .right, .rightMirrored:
  76. ctx?.draw(image.cgImage!, in: CGRect(x: 0, y: 0, width: image.size.height, height: image.size.width))
  77. default:
  78. ctx?.draw(image.cgImage!, in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
  79. }
  80. let cgImage = ctx!.makeImage()
  81. image = UIImage(cgImage: cgImage!)
  82. return image
  83. }
  84. static func createImage(color: UIColor, size: CGSize) -> UIImage? {
  85. var rect = CGRect(origin: CGPoint.zero, size: size)
  86. UIGraphicsBeginImageContext(size)
  87. defer {
  88. UIGraphicsEndImageContext()
  89. }
  90. let context = UIGraphicsGetCurrentContext()
  91. context?.setFillColor(color.cgColor)
  92. context?.fill(rect)
  93. let image = UIGraphicsGetImageFromCurrentImageContext()
  94. return image
  95. }
  96. func resizeImage(_ newSize: CGSize) -> UIImage {
  97. UIGraphicsBeginImageContextWithOptions(newSize, false, UIScreen.main.scale)
  98. self.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
  99. let newImage = UIGraphicsGetImageFromCurrentImageContext()
  100. UIGraphicsEndImageContext()
  101. return newImage!
  102. }
  103. // iOS 9 以后,ImageView.image 设置圆角并不会触发离屏渲染了
  104. // 可以异步绘制,再主线刷新
  105. func imageCornerRadius(_ radius: CGFloat) -> UIImage? {
  106. let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
  107. UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.main.scale);
  108. guard let ctx = UIGraphicsGetCurrentContext() else {
  109. return nil
  110. }
  111. ctx.addPath(UIBezierPath(roundedRect: rect, cornerRadius: radius).cgPath)
  112. ctx.clip()
  113. draw(in: rect)
  114. let image = UIGraphicsGetImageFromCurrentImageContext()
  115. UIGraphicsEndImageContext()
  116. return image
  117. }
  118. func scaledImageFrom3x() -> UIImage {
  119. let theRate: CGFloat = 1.0 / 3.0
  120. let oldSize = self.size
  121. let scaleWidth = CGFloat(oldSize.width) * theRate
  122. let scaleHeight = CGFloat(oldSize.height) * theRate
  123. var scaleRect = CGRect.zero
  124. scaleRect.size = CGSize(width: scaleWidth, height: scaleHeight)
  125. UIGraphicsBeginImageContextWithOptions(scaleRect.size, false, UIScreen.main.scale)
  126. draw(in: scaleRect)
  127. let newImage = UIGraphicsGetImageFromCurrentImageContext()!
  128. UIGraphicsEndImageContext()
  129. return newImage
  130. }
  131. }