Save Generated Barcodes

The Aspose.BarCode Cloud SDK for Swift by the GenerateAPI provides methods to generate barcodes to the Data. Then you can work with result in order to obtain the desired outcomes. This API supports various barcode formats and configurations, enabling users to customize barcode generation to fit their needs.

Methods

  • generate: Generates a barcode via GET request.
  • generateBody: Generates a barcode via POST request with JSON or XML body.
  • generateMultipart: Generates a barcode via POST request with multipart form data. All of these methods return a Data.

Saving the Barcode to File

Below are examples demonstrating how to save the generated barcode stream to a file.

Example: Using generate

import AsposeBarcodeCloud
import Foundation

let client = AsposeBarcodeCloudClient(
    clientId: "Client Id from https://dashboard.aspose.cloud/applications",
    clientSecret: "Client Secret from https://dashboard.aspose.cloud/applications"
)

let imageData = try await GenerateAPI.generate(
    barcodeType: .qr,
    data: "Aspose.BarCode Cloud",
    barcodeImageParams: BarcodeImageParams(imageFormat: .png),
    apiConfiguration: client.apiConfiguration
)

try imageData.write(to: URL(fileURLWithPath: "barcode.png"))
print("File 'barcode.png' generated.")

Download Result Image

Example: Using generateBody

import AsposeBarcodeCloud
import Foundation

let client = AsposeBarcodeCloudClient(
    clientId: "Client Id from https://dashboard.aspose.cloud/applications",
    clientSecret: "Client Secret from https://dashboard.aspose.cloud/applications"
)

let generateParams = GenerateParams(
    barcodeType: .qr,
    encodeData: EncodeData(dataType: .stringData, data: "Aspose.BarCode Cloud"),
    barcodeImageParams: BarcodeImageParams(imageFormat: .jpeg)
)

let imageData = try await GenerateAPI.generateBody(
    generateParams: generateParams,
    apiConfiguration: client.apiConfiguration
)

try imageData.write(to: URL(fileURLWithPath: "qrcode.jpeg"))
print("File 'qrcode.jpeg' generated.")

Download Result Image

Example: Using generateMultipart

import AsposeBarcodeCloud
import Foundation

let client = AsposeBarcodeCloudClient(
    clientId: "Client Id from https://dashboard.aspose.cloud/applications",
    clientSecret: "Client Secret from https://dashboard.aspose.cloud/applications"
)

let imageData = try await GenerateAPI.generateMultipart(
    barcodeType: .pdf417,
    data: "Aspose.BarCode Cloud",
    barcodeImageParams: BarcodeImageParams(imageFormat: .png),
    apiConfiguration: client.apiConfiguration
)

try imageData.write(to: URL(fileURLWithPath: "pdf417.png"))
print("File 'pdf417.png' generated.")

Download Result Image

Conclusion

The Aspose.BarCode Cloud SDK for Swift offers a robust solution for generating barcodes in various formats, with flexible configuration options. By using the provided methods, developers can easily create and save barcode images to files, streamlining workflows that require barcode generation. Whether you’re working with simple barcodes like Code128 or more complex ones like QR Codes and DataMatrix, this API provides the tools needed to customize and produce high-quality barcode images.