HomeiOS DevelopmentConstruct a Barcode Generator Utilizing SwiftUI

Construct a Barcode Generator Utilizing SwiftUI


I not too long ago obtained a query from a buddy relating to barcode era utilizing Swift. The CoreImage framework supplies handy built-in APIs for creating numerous forms of barcodes, together with QR codes. On this tutorial, we’ll discover how one can leverage SwiftUI and these highly effective APIs to develop your very personal barcode generator.

Let’s first check out the ultimate outcome. It’s a really elementary barcode era app with easy UIs. Whenever you enter textual content into the designated area, the app immediately generates the corresponding barcode utilizing the Code 128 format, which is used for alphanumeric or numeric-only barcodes.

swiftui-barcode-generator

Creating the Barcode Generator

Assuming you have got created a brand new SwiftUI mission in Xcode, open the ContentView.swift file. Begin by importing the required bundle for utilizing the filters:

Subsequent, we create a BarcodeGenerator struct for producing the barcode:

To create the barcode, we declare two variables: the context and the Code 128 generator. When you discover the CIFilter class, you’ll uncover different code turbines such because the QR code generator and the Aztec code generator. Nonetheless, for this demonstration, we’ll give attention to utilizing the Code 128 barcode generator.

The generateBarcode methodology accepts a string enter and returns the generated barcode picture. Inside the methodology, we first initialize the code128BarcodeGenerator and assign the enter textual content to its message property. Because the message property expects Information, we convert the enter textual content to Information. Subsequently, we retrieve the generated barcode picture from the outputImage property of the generator.

For the reason that ensuing picture is of kind CIImage, we make the most of the createCGImage methodology of the context to transform it to a CGImage. We then proceed with further steps to transform it into an Picture.

Constructing the Person Interface

Now that now we have completed constructing the barcode generator, let’s transfer on to growing the person interface for displaying the barcode picture.

First, declare the next properties in ContentView for the enter textual content and the barcode generator:

For the person interface, let’s preserve it easy and lay out all of the views in a VStack like this:

On the display, now we have a textual content area that captures person enter. In the direction of the underside of the display, the app shows the generated barcode. In case there isn’t a person enter, we present a default picture with the caption “Unknown information.”

That’s it! It is best to now be capable of take a look at the app within the preview pane. Merely enter any textual content within the textual content area, and the app will robotically generate the barcode on the fly.

swiftui-barcode-generator-code-128

Abstract

This tutorial supplies a complete information on constructing a barcode generator utilizing the CoreImage framework and SwiftUI. Whereas the main target is on making a Code 128 barcode, you’ll be able to simply modify the code to help a number of forms of barcodes together with QR codes.


Founding father of AppCoda. Creator of a number of iOS programming books together with Starting iOS Programming with Swift and Mastering SwiftUI. iOS App Developer and Blogger. Comply with me at Fb, Twitter and Google+.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments