QRGeneratorView.swift (3330B)
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 var body: some View { 32 ZStack { 33 if let image { 34 Image(uiImage: image) 35 .interpolation(.none) 36 .resizable() 37 .scaledToFit() 38 .frame(width: size, height: size) 39 if let logo { 40 logo 41 .resizable() 42 .scaledToFit() 43 .frame(width: logoSize, height: logoSize) 44 } 45 } else { 46 EmptyView() 47 } 48 } 49 .accessibilityElement(children: .combine) 50 .accessibilityLabel(Text("QR Code", comment: "a11y")) 51 .onAppear { 52 // if let uiImage = getQRCode(text: text) { 53 if let data = getQRCodeData(text: text) { 54 if let uiImage = UIImage(data: data) { 55 image = uiImage 56 } 57 } 58 } 59 } 60 61 // func getQRCode(text: String) -> UIImage? { 62 func getQRCodeData(text: String) -> Data? { 63 guard let filter = CIFilter(name: "CIQRCodeGenerator") else { return nil } 64 let data = text.data(using: .ascii, allowLossyConversion: false) 65 filter.setValue(data, forKey: "inputMessage") 66 filter.setValue("Q", forKey: "inputCorrectionLevel") // 4 levels: L M Q H 67 guard let ciimage = filter.outputImage else { return nil } 68 let transform = CGAffineTransform(scaleX: 10, y: 10) 69 let scaledCIImage = ciimage.transformed(by: transform) 70 let uiimage = UIImage(ciImage: scaledCIImage) 71 return uiimage.pngData()! 72 } 73 } 74 75 //#Preview { // 'Previewable()' is only available in iOS 17.0 or newer 76 // @Previewable @State var image: UIImage? 77 // VStack { 78 // QRGeneratorView(text: "Hello World!", size: 200, image: $image) 79 // QRGeneratorView(text: "taler://pay-pull/exchange.demo.taler.net/7J7SNHYMCCAZ1ARY9YCB5Z9FTY0YZP8F2KDRXV94KZCQ6WAVMTX0", 80 // size: 200, image: $image) 81 // } 82 //}