QRCodeGenerator.swift 3.4 KB

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