Ios qr code generation, ios
This blog will introduce the generation of QR codes.
Since there is nothing worth a long term, I will introduce the QR code through code implementation.
Part 1
The first part is the simple generation of QR codes.
Code Section
/// ViewController. m // cx qr code generation /// Created by ma c on 16/4/12. // Copyright©2016 bjsxt. all rights reserved. // # import "ViewController. h "# import <CoreImage/CoreImage. h> @ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad];}-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {// create a filter CIFilter * filter = [CIFilter filterWithName: @ "CIQRCodeGenerator"]; // restore the default setting [filter setDefaults]; // Add data to the filter NSString * str = @ "xubao loves fish"; // The text here can be URL NSData * data = [str dataUsingEncoding: NSUTF8StringEncoding]; [filter setValue: data forKey: @ "inputMessage"]; // output the obtained QR code CIImage * image = [filter outputImage]; // display the QR code UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (20, 20,100,100)]; imageView. image = [UIImage imageWithCIImage: image]; [self. view addSubview: imageView];} @ end
Although it is very simple, it is worth noting that the QR code generated by the method in the native framework is CI, so we need to deal with it before display.
The first part of the scan is a piece of text. Of course, if you add a url according to the annotation, you can also pop up the website.
If you observe it carefully, it is difficult to find that the binary code is not of high definition. The following describes how to increase the definition.
Part 2
The second section describes how to increase the definition of a QR code.
Code Section
/// ViewController. m // cx qr code generation /// Created by ma c on 16/4/12. // Copyright©2016 bjsxt. all rights reserved. // # import "ViewController. h "# import <CoreImage/CoreImage. h> @ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad];}-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {// create a filter CIFilter * filter = [CIFilter filterWithName: @ "CIQRCodeGenerator"]; // restore the default setting [filter setDefaults]; // Add data to the filter NSString * str = @ "xubao loves fish"; // The text here can be URL NSData * data = [str dataUsingEncoding: NSUTF8StringEncoding]; [filter setValue: data forKey: @ "inputMessage"]; // output the obtained QR code CIImage * image = [filter outputImage]; // display the QR code UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (20, 20,100,100)]; imageView. image = [self createNonInterpolatedUIImageFormCIImage: image withSize: 200]; [self. view addSubview: imageView];}/*** generate a UIImage of the specified size based on CIImage ** @ param image CIImage * @ param size image Width */-(UIImage *) createNonInterpolatedUIImageFormCIImage :( CIImage *) image withSize :( CGFloat) size {CGRect extent = CGRectIntegral (image. extent); CGFloat scale = MIN (size/CGRectGetWidth (extent), size/CGRectGetHeight (extent); // 1. create bitmap; size_t width = CGRectGetWidth (extent) * scale; size_t height = CGRectGetHeight (extent) * scale; CGColorSpaceRef cs = limit (); CGContextRef bitmapRef = limit (nil, width, height, 8, 0, cs, (CGBitmapInfo) kCGImageAlphaNone); CIContext * context = [CIContext contextwittions: nil]; CGImageRef bitmapImage = [context createCGImage: image fromRect: extent]; CGContextSetInterpolationQuality (bitmapRef, kCGInterpolationNone); CGContextScaleCTM (bitmapRef, scale, scale); CGContextDrawImage (bitmapRef, extent, bitmapImage); // 2. save bitmap to the image CGImageRef scaledImage = CGBitmapContextCreateImage (bitmapRef); CGContextRelease (bitmapRef); CGImageRelease (bitmapImage); return [UIImage imageWithCGImage: scaledImage ];}
It is worth noting that this method can be changed to a category.
After these steps are completed, we need to think about how to do it when there are images in the middle of the QR code ???
It's easy to get an image and overwrite it in the middle of the QR code (note that the image size is good)