(upbeat music)
- [Steven] QR codes are all the rage.
Let's learn how to generate them from your app.
I've already set up a storyboard of a simple app
to display a square image view.
I also set up a function to make a QR code
so let's go over to the view controller here.
I'll close this up and you could see right here
I've got my outlet,
I've got a qrCodeImage that I've made optional,
and right under that I've got that QR code.
And the first thing I'm going to do,
just for good cleaning purposes is
I'm going to make that qrCodeImage nil.
Now, if the text is going to be empty,
I'm going to return with a blank image.
And so we're going to do if text isEmpty, return,
so we don't spend a lot of time processing
and we generate the QR codes and other barcodes
as CI filters.
So I'm going to have to make a filter.
The initializer for the filter returns an optional.
So I'm going to use guard to unwrap it,
So G-U-A-R-D and then let filter equal
and I'm going to use a C-I-Q-R-Q generator filter.
So I'm going to make it, CIFilter name
and then in quotes,
CIQRCodeGenerator
and then just return if it fails that.
I'm going to need another guard
to change the text to data format.
So I'm going to do another guard here.
I'll take the text and go to data using,
and I'll usually allowLossyConversion here
and I'm going to put in the using isoLatin1,
'cause that's the preferred one, according to Apple.
And then right here, for allowLossyConversion,
I want to make that false.
Then we'll have to do an else, return.
So I've got my filter, I've got my data.
And now I'm going to set the two values for the filter
and the first one will be the data itself.
And I'm going to do filter setValue,
my value is going to be my data
and the key here is inputMessage.
I got one more value to set for this
and that's the input correction level.
There's several levels L for low M for medium Q for quality
and H for high.
This adds extra data to the QR code to deal with dirt
and other corruption of the image.
As the quality increases, the amount of data increases.
So I'm going to go for M for medium in this case.
And so I'm going to do filter setValue
and my value is going to be an M.
And the key here is the inputCorrectionLevel.
And now I'm ready to get the image from the filter.
So I'll do qrCodeImage equals filter.outputImage
and that's all optional with a QR code image
ending up as an optional value.
So I'm going to do an if let here,
to unwrap it.
And then inside here,
I'm going to now put this in the imageView.
And I'm going to need to make this a UI image.
So I'm going to say UIimage here
and then I need a ciImage, which is right here,
and I can put the qrCodeImage, like that.
Now, if you head on down to viewDidLoad,
you'll see that I've already put in here a URL,
which is one of the things
you'll probably use these for the most
but this could be any texts.
I can put, I love pizza in here, for example.
Go ahead and set your simulator, iPhone XR.
Then go ahead and run.
If you have a phone handy,
open the camera app or the QR code app,
which you'll find on the control panel, if you've set it up,
but it's really just the camera app.
So I'm just going to use the camera here
and pointed at your QR code.
And you'll see almost immediately,
you get a notification
that says it wants to open something.
And I'm going to go ahead and hit the notification
and it goes into LinkedIn Learning and the tips.
You can change the filter to other formats.
These include a variety of 1D barcodes
and 2D with data matrices.
With these filters,
many of your barcode needs can easily be generated
in your apps.
(upbeat music)