Swift से QR कोड जनरेट करें
Swift CoreImage के CIQRCodeGenerator फ़िल्टर के माध्यम से बिल्ट-इन QR कोड जनरेशन प्रदान करता है। iOS और macOS ऐप्स के लिए कोई थर्ड-पार्टी डिपेंडेंसी नहीं चाहिए।
सेटअप
CoreImage iOS और macOS SDK में शामिल है। कोई अतिरिक्त इंस्टॉलेशन आवश्यक नहीं।
import CoreImage // No installation needed — included in iOS & macOS SDKCoreImage से QR कोड जनरेट करें
Swift के बिल्ट-इन CIQRCodeGenerator का उपयोग करने के कोड उदाहरण।
import CoreImage
import UIKit
func generateQRCode(from string: String) -> UIImage? {
let data = string.data(using: .ascii)
guard let filter = CIFilter(name: "CIQRCodeGenerator") else { return nil }
filter.setValue(data, forKey: "inputMessage")
filter.setValue("Q", forKey: "inputCorrectionLevel")
guard let ciImage = filter.outputImage else { return nil }
// Scale up for crisp rendering
let transform = CGAffineTransform(scaleX: 10, y: 10)
let scaledImage = ciImage.transformed(by: transform)
return UIImage(ciImage: scaledImage)
}
let qrImage = generateQRCode(from: "https://qrcode.fun")func generateColoredQRCode(from string: String,
foreground: UIColor = .black,
background: UIColor = .white) -> UIImage? {
let data = string.data(using: .ascii)
guard let qrFilter = CIFilter(name: "CIQRCodeGenerator"),
let colorFilter = CIFilter(name: "CIFalseColor") else { return nil }
qrFilter.setValue(data, forKey: "inputMessage")
qrFilter.setValue("H", forKey: "inputCorrectionLevel")
colorFilter.setValue(qrFilter.outputImage, forKey: "inputImage")
colorFilter.setValue(CIColor(color: foreground), forKey: "inputColor0")
colorFilter.setValue(CIColor(color: background), forKey: "inputColor1")
guard let output = colorFilter.outputImage else { return nil }
let transform = CGAffineTransform(scaleX: 10, y: 10)
return UIImage(ciImage: output.transformed(by: transform))
}Swift में API से QR कोड जनरेट करें
कस्टम रंग और लोगो के साथ स्टाइल किए गए QR कोड के लिए Swift से URLSession का उपयोग करके QRCode.fun API कॉल करें।
import Foundation
func generateQRCodeViaAPI(data: String) async throws -> Data {
let url = URL(string: "https://qrcode.fun/api/generate-qr-styled")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let payload: [String: Any] = [
"data": data,
"width": 300,
"height": 300,
"type": "png",
"margin": 10,
"dotsOptions": ["color": "#1A2B3C", "type": "rounded"],
"cornersSquareOptions": ["color": "#8564C3", "type": "extra-rounded"],
"backgroundOptions": ["color": "#FFFFFF"]
]
request.httpBody = try JSONSerialization.data(withJSONObject: payload)
let (responseData, _) = try await URLSession.shared.data(for: request)
return responseData // Parse JSON to get base64 image
}
// Usage
let imageData = try await generateQRCodeViaAPI(data: "https://qrcode.fun")लाइव QR कोड प्रीव्यू
अभी Swift से QR कोड जनरेट करके देखें।
CoreImage बनाम API
CoreImage सीधे उपयोग करने बनाम QRCode.fun API की तुलना।
| फीचर | CoreImage | QRCode.fun API |
|---|---|---|
| सेटअप जटिलता | बिल्ट-इन, इंस्टॉल नहीं चाहिए | URLSession से HTTP रिक्वेस्ट |
| कस्टमाइज़ेशन | बेसिक (CIFilter से टिंटिंग) | पूर्ण स्टाइलिंग: रंग, आकार, लोगो |
| ऑफ़लाइन सपोर्ट | हाँ | इंटरनेट आवश्यक |
| रखरखाव | iOS/macOS SDK का हिस्सा | हमेशा अपडेटेड |
| आउटपुट फॉर्मेट | CIImage → UIImage/NSImage | PNG, SVG |
Swift QR कोड उपयोग के मामले
Swift एप्लिकेशन में QR कोड के लिए सामान्य परिदृश्य।
iOS ऐप्स
iPhone और iPad ऐप में लिंक, कॉन्टैक्ट, Wi-Fi क्रेडेंशियल और ऐप डीप लिंक शेयर करने के लिए QR कोड जनरेट करें।
SwiftUI व्यूज़
कस्टम स्टाइलिंग और एनिमेशन के साथ QR कोड रेंडर करने वाले रीयूज़ेबल SwiftUI कंपोनेंट बनाएं।
macOS ऐप्स
बिज़नेस कार्ड, लेबल और डॉक्यूमेंट मैनेजमेंट के लिए QR कोड जनरेट करने वाले macOS टूल बनाएं।
Apple Watch
बोर्डिंग पास, टिकट और लॉयल्टी कार्ड तक त्वरित पहुंच के लिए Apple Watch पर QR कोड प्रदर्शित करें।
अक्सर पूछे जाने वाले प्रश्न
Swift से QR कोड जनरेट करने के बारे में सामान्य प्रश्न।
Swift से QR कोड जनरेट करना शुरू करें
हमारा मुफ़्त जनरेटर उपयोग करें या API को अपनी iOS और macOS ऐप्स में एकीकृत करें।