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