UIImage+Extension.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import UIKit
  2. extension UIImage {
  3. func imageResize(sizeChange: CGSize) -> UIImage {
  4. let hasAlpha = true
  5. let scale: CGFloat = 0.0 // Use scale factor of main screen
  6. UIGraphicsBeginImageContextWithOptions(sizeChange, !hasAlpha, scale)
  7. draw(in: CGRect(origin: CGPoint.zero, size: sizeChange))
  8. let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
  9. return scaledImage!
  10. }
  11. public convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
  12. let rect = CGRect(origin: .zero, size: size)
  13. UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
  14. color.setFill()
  15. UIRectFill(rect)
  16. let image = UIGraphicsGetImageFromCurrentImageContext()
  17. UIGraphicsEndImageContext()
  18. guard let cgImage = image?.cgImage else { return nil }
  19. self.init(cgImage: cgImage)
  20. }
  21. func resizeImage(targetSize: CGSize) -> UIImage? {
  22. let size = self.size
  23. let widthRatio = targetSize.width / size.width
  24. let heightRatio = targetSize.height / size.height
  25. var newSize: CGSize
  26. if widthRatio > heightRatio {
  27. newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
  28. } else {
  29. newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
  30. }
  31. let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
  32. UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
  33. draw(in: rect)
  34. let newImage = UIGraphicsGetImageFromCurrentImageContext()
  35. UIGraphicsEndImageContext()
  36. return newImage
  37. }
  38. func dcCompress(toMax target: Float = 1280) -> UIImage? {
  39. return resize(toMax: target)
  40. }
  41. func imageSizeInPixel() -> CGSize {
  42. let heightInPoints = size.height
  43. let heightInPixels = heightInPoints * scale
  44. let widthInPoints = size.width
  45. let widthInPixels = widthInPoints * scale
  46. return CGSize(width: widthInPixels, height: heightInPixels)
  47. }
  48. // source: https://stackoverflow.com/questions/29137488/how-do-i-resize-the-uiimage-to-reduce-upload-image-size // slightly changed
  49. func resize(toMax: Float) -> UIImage? {
  50. var actualHeight = Float(size.height)
  51. var actualWidth = Float(size.width)
  52. let maxHeight: Float = toMax
  53. let maxWidth: Float = toMax
  54. var imgRatio: Float = actualWidth / actualHeight
  55. let maxRatio: Float = maxWidth / maxHeight
  56. let compressionQuality: Float = 0.5
  57. //50 percent compression
  58. if actualHeight > maxHeight || actualWidth > maxWidth {
  59. if imgRatio < maxRatio {
  60. //adjust width according to maxHeight
  61. imgRatio = maxHeight / actualHeight
  62. actualWidth = imgRatio * actualWidth
  63. actualHeight = maxHeight
  64. } else if imgRatio > maxRatio {
  65. //adjust height according to maxWidth
  66. imgRatio = maxWidth / actualWidth
  67. actualHeight = imgRatio * actualHeight
  68. actualWidth = maxWidth
  69. } else {
  70. actualHeight = maxHeight
  71. actualWidth = maxWidth
  72. }
  73. }
  74. let rect = CGRect(x: 0.0, y: 0.0, width: CGFloat(actualWidth), height: CGFloat(actualHeight))
  75. UIGraphicsBeginImageContext(rect.size)
  76. draw(in: rect)
  77. let img = UIGraphicsGetImageFromCurrentImageContext()
  78. let imageData = img?.jpegData(compressionQuality: CGFloat(compressionQuality))
  79. UIGraphicsEndImageContext()
  80. return UIImage(data: imageData!)
  81. }
  82. public class func messageKitImageWith(type: ImageType) -> UIImage? {
  83. let assetBundle = Bundle.messageKitAssetBundle()
  84. let imagePath = assetBundle.path(forResource: type.rawValue, ofType: "png", inDirectory: "Images")
  85. let image = UIImage(contentsOfFile: imagePath ?? "")
  86. return image
  87. }
  88. }
  89. public enum ImageType: String {
  90. case play
  91. case pause
  92. case disclouser
  93. }