QRCodeGenerator.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // LoopFollow
  2. // QRCodeGenerator.swift
  3. // Created by codebymini.
  4. import CoreImage
  5. import UIKit
  6. enum QRCodeGenerator {
  7. /// Generates a QR code image from a string
  8. /// - Parameters:
  9. /// - string: The string to encode in the QR code
  10. /// - size: The size of the generated image (default: 200x200)
  11. /// - correctionLevel: The error correction level (default: .M)
  12. /// - Returns: A UIImage containing the QR code, or nil if generation fails
  13. static func generateQRCode(
  14. from string: String,
  15. size: CGSize = CGSize(width: 200, height: 200),
  16. correctionLevel: String = "M"
  17. ) -> UIImage? {
  18. // Create a CIFilter for QR code generation
  19. guard let filter = CIFilter(name: "CIQRCodeGenerator") else {
  20. return nil
  21. }
  22. // Set the input data (the string to encode)
  23. let data = string.data(using: .utf8)
  24. filter.setValue(data, forKey: "inputMessage")
  25. // Set the error correction level
  26. filter.setValue(correctionLevel, forKey: "inputCorrectionLevel")
  27. // Get the output image
  28. guard let outputImage = filter.outputImage else {
  29. return nil
  30. }
  31. // Scale the image to the desired size
  32. let scaleX = size.width / outputImage.extent.size.width
  33. let scaleY = size.height / outputImage.extent.size.height
  34. let scaledImage = outputImage.transformed(by: CGAffineTransform(scaleX: scaleX, y: scaleY))
  35. // Convert CIImage to UIImage
  36. let context = CIContext()
  37. guard let cgImage = context.createCGImage(scaledImage, from: scaledImage.extent) else {
  38. return nil
  39. }
  40. return UIImage(cgImage: cgImage)
  41. }
  42. /// Generates a QR code image with custom colors
  43. /// - Parameters:
  44. /// - string: The string to encode in the QR code
  45. /// - size: The size of the generated image (default: 200x200)
  46. /// - foregroundColor: The color of the QR code (default: black)
  47. /// - backgroundColor: The background color (default: white)
  48. /// - correctionLevel: The error correction level (default: .M)
  49. /// - Returns: A UIImage containing the QR code, or nil if generation fails
  50. static func generateQRCode(
  51. from string: String,
  52. size: CGSize = CGSize(width: 200, height: 200),
  53. foregroundColor: UIColor = .black,
  54. backgroundColor: UIColor = .white,
  55. correctionLevel: String = "M"
  56. ) -> UIImage? {
  57. // First generate the basic QR code
  58. guard let qrCodeImage = generateQRCode(from: string, size: size, correctionLevel: correctionLevel) else {
  59. return nil
  60. }
  61. // Create a new image context with the desired size
  62. UIGraphicsBeginImageContextWithOptions(size, false, 0)
  63. defer { UIGraphicsEndImageContext() }
  64. guard let context = UIGraphicsGetCurrentContext() else {
  65. return nil
  66. }
  67. // Fill the background
  68. backgroundColor.setFill()
  69. context.fill(CGRect(origin: .zero, size: size))
  70. // Draw the QR code with the foreground color
  71. context.setFillColor(foregroundColor.cgColor)
  72. context.setBlendMode(.sourceIn)
  73. // Create a mask from the original QR code
  74. if let cgImage = qrCodeImage.cgImage {
  75. let maskImage = UIImage(cgImage: cgImage)
  76. maskImage.draw(in: CGRect(origin: .zero, size: size))
  77. }
  78. return UIGraphicsGetImageFromCurrentImageContext()
  79. }
  80. }