QRGeneratorView.swift (3493B)
1 /* MIT License 2 * Copyright (c) 2020 Jeeva Tamilselvan 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a copy 5 * of this software and associated documentation files (the "Software"), to deal 6 * in the Software without restriction, including without limitation the rights 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 * copies of the Software, and to permit persons to whom the Software is 9 * furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in all 12 * copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 * SOFTWARE. 21 */ 22 import SwiftUI 23 24 public func QRCode(from text: String) -> UIImage? { 25 guard let filter = CIFilter(name: "CIQRCodeGenerator") else { return nil } 26 let data = text.data(using: .utf8, allowLossyConversion: false) 27 filter.setValue(data, forKey: "inputMessage") 28 filter.setValue("Q", forKey: "inputCorrectionLevel") // 4 levels: L M Q H 29 guard let ciimage = filter.outputImage else { return nil } 30 let transform = CGAffineTransform(scaleX: 10, y: 10) 31 let scaledCIImage = ciimage.transformed(by: transform) 32 // a UIImage(ciImage:) has no bitmap representation, so pngData() would return nil 33 guard let cgImage = CIContext().createCGImage(scaledCIImage, 34 from: scaledCIImage.extent) else { return nil } 35 return UIImage(cgImage: cgImage) 36 } 37 // MARK: - 38 struct QRGeneratorView: View { 39 let image: UIImage? 40 let size: CGFloat 41 let logo: Image? 42 let logoSize: CGFloat 43 44 // a QR code which cannot be generated must not look like an empty area to scan 45 private var failedText: Text { 46 Text("Cannot create QR code", comment: "shown instead of the QR code") 47 } 48 49 var body: some View { 50 ZStack { 51 if let image { 52 Image(uiImage: image) 53 .interpolation(.none) 54 .resizable() 55 .scaledToFit() 56 .frame(width: size, height: size) 57 if let logo { 58 logo 59 .resizable() 60 .scaledToFit() 61 .frame(width: logoSize * 2, height: logoSize) 62 } 63 } else { 64 failedText 65 .talerFont(.title2) 66 .frame(width: size, height: size) 67 } 68 } 69 .accessibilityElement(children: .combine) 70 .accessibilityLabel(image == nil ? failedText : Text("QR Code", comment: "a11y")) 71 } 72 } 73 74 //#Preview { // 'Previewable()' is only available in iOS 17.0 or newer 75 // @Previewable @State var image: UIImage? 76 // VStack { 77 // QRGeneratorView(text: "Hello World!", size: 200, image: $image) 78 // QRGeneratorView(text: "taler://pay-pull/exchange.demo.taler.net/7J7SNHYMCCAZ1ARY9YCB5Z9FTY0YZP8F2KDRXV94KZCQ6WAVMTX0", 79 // size: 200, image: $image) 80 // } 81 //}