IOS native CIFilter creates a QR code, ioscifilter

Source: Internet
Author: User

IOS native CIFilter creates a QR code, ioscifilter
IOS native CIFilter creates a QR code future C

Create a QR code for iOS native CIFilter

 

 

There are a lot of online QR code generation websites. Most third-party libraries have been written through C ++ a long time ago, and some are also quite useful, such as zxing. Here we will introduce how to create a QR code through CIFilter.

 

 

 

It is very easy to create a QR code. Just input a simple string.
-(CIImage *) createQRForString :( NSString *) qrString {NSData * stringData = [qrString dataUsingEncoding: encoding]; // create a filter CIFilter * qrFilter = [CIFilter filterWithName: @ "CIQRCodeGenerator"]; // sets the content and Error Correction level [qrFilter setValue: stringData forKey: @ "inputMessage"]; [qrFilter setValue: @ "M" forKey: @ "inputCorrectionLevel"]; // return CIImage return qrFilter. outputImage ;}

 

 

 

The generated QR code is a CIImage. If we convert it directly to a UIImage, the size cannot be controlled. Therefore, the following method is used to return the UIImage with the required size:

 

-(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 the bitmap to the image CGImageRef scaledImage = watermark (bitmapRef); // the original image UIImage * outputImage = [UIImage imageWithCGImage: scaledImage]; outputs (outputImage. size, NO, [[UIScreen mainScreen] scale]); [outputImage drawInRect: CGRectMake (0, 0, size, size)]; // watermark image UIImage * waterimage = [UIImage imageNamed: @ "icon"]; [waterimage drawInRect: CGRectMake (size-waterImagesize)/2.0, (size-waterImagesize)/2.0, waterImagesize, waterImagesize)]; UIImage * newPic = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); return newPic ;}

 

 

 

 

Future CTO
Follow my CTO path from now on

No.: wlaicto

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.