Search code examples
swiftuiimageswiftui

SwiftUI: UIImage (QRCode) does not load after calling function


I have spent a lot of time trying to diagnose this issue and I am totally stuck, perhaps I am missing something very obvious.

Already tried:

  • Saving the returned image to disk to see if it was being generated correctly (it was)
  • Adding a colored background renders a square, so something is being loaded
  • Marking the image with .renderingMode(.original)
  • Using @State var img = UIImage() and reassigning usingImage(uiImage:img).onAppear { self.img = self.generateQRCode(from: self.qrCodeString)! }

Any help would be sincerely appreciated!

import SwiftUI

struct ContentView: View {

    let qrCodeString = "BEGIN:VCARD \n" +
    "VERSION:2.1 \n" +
    "FN:John Peter \n" +
    "N:Peter;John \n" +
    "TITLE:Admin \n" +
    "TEL;CELL:+91 431 524 2345 \n" +
    "TEL;WORK;VOICE:+91 436 542 8374 \n" +
    "EMAIL;WORK;INTERNET:[email protected] \n" +
    "URL:www.facebook.com \n" +
    "URL: www.instagram.com \n" +
    "ADR;WORK:;;423 ofce sales Center;Newark;DE;3243;USA \n" +
    "ORG:xxx Private limited \n" +
    "END:VCARD"

    var body: some View {
        Image(uiImage: generateQRCode(from: qrCodeString)!)
    }

    func generateQRCode(from string: String) -> UIImage? {
        let data = string.data(using: String.Encoding.ascii)

        if let filter = CIFilter(name: "CIQRCodeGenerator") {
            filter.setValue(data, forKey: "inputMessage")
            let transform = CGAffineTransform(scaleX: 3, y: 3)

            if let output = filter.outputImage?.transformed(by: transform) {

            return UIImage(ciImage: output)
            }
        }
        return nil
    }
}

Solution

  • This seems to do the trick:

    func generateQRCode(from string: String) -> UIImage? {
    
        let data = string.data(using: String.Encoding.ascii)
        var uiImage: UIImage?
        if let filter = CIFilter(name: "CIQRCodeGenerator",
                                 parameters: ["inputMessage": data,
                                              "inputCorrectionLevel": "L"]) {
    
            if let outputImage = filter.outputImage,
                let cgImage = CIContext().createCGImage(outputImage,
                                                        from: outputImage.extent) {
                let size = CGSize(width: outputImage.extent.width * 3.0,
                                  height: outputImage.extent.height * 3.0)
                UIGraphicsBeginImageContext(size)
                if let context = UIGraphicsGetCurrentContext() {
                    context.interpolationQuality = .none
                    context.draw(cgImage,
                                 in: CGRect(origin: .zero,
                                            size: size))
                    uiImage = UIGraphicsGetImageFromCurrentImageContext()
                }
                UIGraphicsEndImageContext()
            }
        }
        return uiImage
    }