Sample code for 30+ languages & platforms
Swift Requires Chilkat v11.7.0+

Generate a QR Code PNG in Memory

See more QR Code Examples

Demonstrates getting a QR code image without writing a file. With no output options the result is a PNG returned as base64 in outputs[0].base64 of the response. The example decodes it into a BinData, which holds the bytes of the PNG to send in an HTTP response, store in a database, attach to an email, or save.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    let json = CkoJsonObject()!
    json.updateString(jsonPath: "payload.text", value: "https://www.chilkatsoft.com/")
    json.updateInt(jsonPath: "render.moduleSize", value: 6)

    let fmt = CkoFormats()!
    let jsonOut = CkoJsonObject()!
    success = fmt.qrCode(op: "generate", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("\(fmt.lastErrorText!)")
        return
    }

    print("format: \(jsonOut.string(of: "outputs[0].format")!)")
    print("MIME type: \(jsonOut.string(of: "outputs[0].mimeType")!)")
    print("image: \(jsonOut.int(of: "outputs[0].width").intValue) x \(jsonOut.int(of: "outputs[0].height").intValue) pixels")

    //  outputs[0].base64 is the PNG file, base64 encoded.
    let bdPng = CkoBinData()!
    success = bdPng.appendEncoded(encData: jsonOut.string(of: "outputs[0].base64"), encoding: "base64")
    if success == false {
        print("\(bdPng.lastErrorText!)")
        return
    }

    print("PNG bytes: \(bdPng.numBytes.intValue)")

    //  The BinData holds the image.  Here it is simply saved.
    success = bdPng.writeFile(path: "qa_output/qr_in_memory.png")
    if success == false {
        print("\(bdPng.lastErrorText!)")
        return
    }

    //  The PNG held in bdPng:

    //  image

    //  Sample output:

    //  format: png
    //  MIME type: image/png
    //  image: 222 x 222 pixels
    //  PNG bytes: 877

}