QRGeneratorView.swift (3955B)
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 struct QRGeneratorView: View { 25 let text: String 26 let size: CGFloat 27 let logo: Image? 28 let logoSize: CGFloat 29 @Binding var image: UIImage? 30 31 @State private var failed = false 32 33 // a QR code which cannot be generated must not look like an empty area to scan 34 private var failedText: Text { 35 Text("Cannot create QR code", comment: "shown instead of the QR code") 36 } 37 38 var body: some View { 39 ZStack { 40 if let image { 41 Image(uiImage: image) 42 .interpolation(.none) 43 .resizable() 44 .scaledToFit() 45 .frame(width: size, height: size) 46 if let logo { 47 logo 48 .resizable() 49 .scaledToFit() 50 .frame(width: logoSize * 2, height: logoSize) 51 } 52 } else if failed { 53 failedText 54 .talerFont(.title2) 55 .frame(width: size, height: size) 56 } else { 57 EmptyView() 58 } 59 } 60 .accessibilityElement(children: .combine) 61 .accessibilityLabel(failed ? failedText : Text("QR Code", comment: "a11y")) 62 .onAppear { 63 // if let uiImage = getQRCode(text: text) { 64 if let data = getQRCodeData(text: text), 65 let uiImage = UIImage(data: data) { 66 image = uiImage 67 } else { 68 failed = true 69 } 70 } 71 } 72 73 // func getQRCode(text: String) -> UIImage? { 74 func getQRCodeData(text: String) -> Data? { 75 guard let filter = CIFilter(name: "CIQRCodeGenerator") else { return nil } 76 let data = text.data(using: .ascii, allowLossyConversion: false) 77 filter.setValue(data, forKey: "inputMessage") 78 filter.setValue("Q", forKey: "inputCorrectionLevel") // 4 levels: L M Q H 79 guard let ciimage = filter.outputImage else { return nil } 80 let transform = CGAffineTransform(scaleX: 10, y: 10) 81 let scaledCIImage = ciimage.transformed(by: transform) 82 // a UIImage(ciImage:) has no bitmap representation, so pngData() would return nil 83 guard let cgImage = CIContext().createCGImage(scaledCIImage, from: scaledCIImage.extent) 84 else { return nil } 85 return UIImage(cgImage: cgImage).pngData() 86 } 87 } 88 89 //#Preview { // 'Previewable()' is only available in iOS 17.0 or newer 90 // @Previewable @State var image: UIImage? 91 // VStack { 92 // QRGeneratorView(text: "Hello World!", size: 200, image: $image) 93 // QRGeneratorView(text: "taler://pay-pull/exchange.demo.taler.net/7J7SNHYMCCAZ1ARY9YCB5Z9FTY0YZP8F2KDRXV94KZCQ6WAVMTX0", 94 // size: 200, image: $image) 95 // } 96 //}