UIImageExtension.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import UIKit
  6. extension UIImage {
  7. // Function to overlay an image on top of the current image
  8. @objc func overlay(with overlayImage: UIImage, at overlayRect: CGRect) -> UIImage? {
  9. // Calculate the new size for the resulting image
  10. let newWidth = max(self.size.width, overlayRect.origin.x + overlayRect.size.width)
  11. let newHeight = max(self.size.height, overlayRect.origin.y + overlayRect.size.height)
  12. let newSize = CGSize(width: newWidth, height: newHeight)
  13. // Begin a new image context with the new size.
  14. UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
  15. // Draw the base image in its original position.
  16. self.draw(in: CGRect(origin: CGPoint.zero, size: self.size))
  17. // Draw the overlay image in the specified rectangle.
  18. overlayImage.draw(in: overlayRect)
  19. // Capture the new image from the context.
  20. let newImage = UIGraphicsGetImageFromCurrentImageContext()
  21. // End the image context to free up memory.
  22. UIGraphicsEndImageContext()
  23. return newImage
  24. }
  25. // Function to crop an image into a circle with the specified size.
  26. @objc func cropToCircle(withSize size: CGSize) -> UIImage? {
  27. // Begin a new image context with the target size
  28. UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
  29. // Create a circular path using a rounded rectangle
  30. let rect = CGRect(origin: .zero, size: size)
  31. let path = UIBezierPath(roundedRect: rect, cornerRadius: size.width / 2)
  32. path.addClip()
  33. // Draw the image in the context, scaled to fill the entire circular area
  34. self.draw(in: rect)
  35. // Capture the new image
  36. let circleImage = UIGraphicsGetImageFromCurrentImageContext()
  37. // End the image context to free up memory
  38. UIGraphicsEndImageContext()
  39. return circleImage
  40. }
  41. // Function to add a circular background with specified background color, diameter and padding
  42. @objc func withCircularBackground(backgroundColor: UIColor, diameter: CGFloat, padding: CGFloat) -> UIImage? {
  43. // Begin a new image context with the target diameter as both width and height
  44. UIGraphicsBeginImageContextWithOptions(CGSize(width: diameter, height: diameter), false, 0.0)
  45. // Define the circle's path using the diameter
  46. let circlePath = UIBezierPath(ovalIn: CGRect(origin: .zero, size: CGSize(width: diameter, height: diameter)))
  47. // Set the fill color and fill the circle
  48. backgroundColor.setFill()
  49. circlePath.fill()
  50. // Calculate the frame for the image inside the circle
  51. let imageSize = CGSize(width: diameter - 2 * padding, height: diameter - 2 * padding)
  52. let imageRect = CGRect(
  53. x: (diameter - imageSize.width) / 2,
  54. y: (diameter - imageSize.height) / 2,
  55. width: imageSize.width,
  56. height: imageSize.height
  57. )
  58. // Draw the image inside the calculated frame
  59. self.draw(in: imageRect)
  60. // Capture the final image
  61. let resultImage = UIGraphicsGetImageFromCurrentImageContext()
  62. // End the image context to free up memory
  63. UIGraphicsEndImageContext()
  64. return resultImage
  65. }
  66. // Function to create a UIImage from a UILabel
  67. @objc static func image(from label: UILabel) -> UIImage? {
  68. // Begin a new image context with the size of the label
  69. UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0.0)
  70. // Render the label layer into the current context
  71. label.layer.render(in: UIGraphicsGetCurrentContext()!)
  72. // Capture the image from the context
  73. let image = UIGraphicsGetImageFromCurrentImageContext()
  74. // End the image context to free up memory
  75. UIGraphicsEndImageContext()
  76. return image
  77. }
  78. }