Generating QR Code with SwiftUI shows empty picture

后端 未结 2 710
北荒
北荒 2020-12-19 20:02

im trying to generate a QR code in my app. The problem is that whenever I do the picture is just an empty square. I stripped down the code to the basics to try and show my p

2条回答
  •  抹茶落季
    2020-12-19 20:24

    func returnQRData(str: String) -> Data {
         let filter = CIFilter(name: "CIQRCodeGenerator")
         let data = str.data(using: .ascii, allowLossyConversion: false)
         filter?.setValue(data, forKey: "inputMessage")
         let transform = CGAffineTransform(scaleX: 5, y: 5)
    
    
         let image = filter?.outputImage?.transformed(by: transform)
         let uiImage = UIImage(ciImage: image!)
    
         return uiImage.pngData()!
    }
    
    Image(uiImage: UIImage(data: self.returnQRData(str: "www.apple.com")) ?? UIImage(named: "noImage1")!)
        .resizable()
        .aspectRatio(contentMode: .fit)
        .padding()
    

提交回复
热议问题