taler-ios

iOS apps for GNU Taler (wallet)
Log | Files | Refs | README | LICENSE

QRGeneratorView.swift (2861B)


      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     @Binding var image: UIImage?
     28 
     29     var body: some View {
     30         VStack {
     31             if let image {
     32                 Image(uiImage: image)
     33                     .interpolation(.none)
     34                     .resizable()
     35                     .scaledToFit()
     36                     .frame(width: size, height: size)
     37             } else {
     38                 EmptyView()
     39             }
     40         }.onAppear {
     41 //          if let uiImage = getQRCode(text: text) {
     42             if let data = getQRCodeData(text: text) {
     43                 if let uiImage = UIImage(data: data)  {
     44                     image = uiImage
     45                 }
     46             }
     47         }
     48     }
     49 
     50 //    func getQRCode(text: String) -> UIImage? {
     51     func getQRCodeData(text: String) -> Data? {
     52         guard let filter = CIFilter(name: "CIQRCodeGenerator") else { return nil }
     53         let data = text.data(using: .ascii, allowLossyConversion: false)
     54         filter.setValue(data, forKey: "inputMessage")
     55         guard let ciimage = filter.outputImage else { return nil }
     56         let transform = CGAffineTransform(scaleX: 10, y: 10)
     57         let scaledCIImage = ciimage.transformed(by: transform)
     58         let uiimage = UIImage(ciImage: scaledCIImage)
     59         return uiimage.pngData()!
     60     }
     61 }
     62 
     63 //#Preview {      // 'Previewable()' is only available in iOS 17.0 or newer
     64 //    @Previewable @State var image: UIImage?
     65 //    VStack {
     66 //        QRGeneratorView(text: "Hello World!", size: 200, image: $image)
     67 //        QRGeneratorView(text: "taler://pay-pull/exchange.demo.taler.net/7J7SNHYMCCAZ1ARY9YCB5Z9FTY0YZP8F2KDRXV94KZCQ6WAVMTX0",
     68 //                        size: 200, image: $image)
     69 //    }
     70 //}