Generating QR code pictures is also commonly used in projects. There are many useful QR code scanning on Git. Here we mainly talk about the generation of QR codes.
1. Ordinary QR code
1.1 Method
/** Generate QR code QRStering: String imageFloat: QR code image size */ + (UIImage *)createQRCodeWithString:(NSString *)QRStering withImgSize:(CGFloat)imageFloat;
1.2 Method implementation
/** Generate QR code QRStering: String imageFloat: QR code image size */ + (UIImage *)createQRCodeWithString:(NSString *)QRString withImgSize:(CGFloat)imageFloat{ CIFilter *filter = [CIFilter filterWithName:@"XiaoGuiGe"]; [filter setDefaults]; NSString *getString = QRString; NSData *dataString = [getString dataUsingEncoding:NSUTF8StringEncoding]; [filter setValue:dataString forKey:@"inputMessage"]; ///Get the image output from the filter CIImage *outImage = [filter outputImage]; UIImage *imageV = [self imageWithImageSize:imageFloat withCIIImage:outImage]; //Return to QR code image return imageV; }
2. QR code with small icon in the middle
2.1 Method
/** Generate QR code (with small pictures in the middle) QRStering: String centerImage: image object in the middle of the QR code */ + (UIImage *)createImgQRCodeWithString:(NSString *)QRString centerImage:(UIImage *)centerImage;
2.2 Method implementation
/** Generate QR code (with small pictures in the middle) QRStering: Required string centerImage: image object in the middle of the QR code */ + (UIImage *)createImgQRCodeWithString:(NSString *)QRString centerImage:(UIImage *)centerImage{ // Create filter object CIFilter *filter = [CIFilter filterWithName:@"XiaoGuiGe"]; // Restore the default properties of the filter [filter setDefaults]; // Convert string to NSdata NSData *dataString = [QRString dataUsingEncoding:NSUTF8StringEncoding]; // Set the input value of the filter, KVC assignment [filter setValue:dataString forKey:@"inputMessage"]; // Obtain the image output from the filter CIImage *outImage = [filter outputImage]; // The picture is smaller than (27, 27), we need to enlarge it outImage = [outImage imageByApplyingTransform:CGAffineTransformMakeScale(20, 20)]; // Convert CIImage type to UIImage type UIImage *startImage = [UIImage imageWithCIImage:outImage]; // Turn on drawing to get the graphic context UIGraphicsBeginImageContext(); // Draw the QR code picture (here is the graphic context, the upper left corner is (0,0) point [startImage drawInRect:CGRectMake(0, 0, , )]; // Draw the small picture again CGFloat icon_imageW = 200; CGFloat icon_imageH = icon_imageW; CGFloat icon_imageX = ( - icon_imageW) * 0.5; CGFloat icon_imageY = ( - icon_imageH) * 0.5; [centerImage drawInRect:CGRectMake(icon_imageX, icon_imageY, icon_imageW, icon_imageH)]; // Get the picture you are currently drawing UIImage *qrImage = UIGraphicsGetImageFromCurrentImageContext(); // Close the graphic context UIGraphicsEndImageContext(); //Return to QR code image return qrImage; }
Additional Methods
/** Convert CIImage to UIImage and zoom in (used by internal conversion)*/ + (UIImage *)imageWithImageSize:(CGFloat)size withCIIImage:(CIImage *)ciiImage{ CGRect extent = CGRectIntegral(); CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent)); // 1. Create a bitmap; size_t width = CGRectGetWidth(extent) * scale; size_t height = CGRectGetHeight(extent) * scale; CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray(); CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone); CIContext *context = [CIContext contextWithOptions:nil]; CGImageRef bitmapImage = [context createCGImage:ciiImage fromRect:extent]; CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone); CGContextScaleCTM(bitmapRef, scale, scale); CGContextDrawImage(bitmapRef, extent, bitmapImage); // 2.Save bitmap to picture CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef); CGContextRelease(bitmapRef); CGImageRelease(bitmapImage); return [UIImage imageWithCGImage:scaledImage]; }
Summarize
The above is the iOS development-generated QR code picture (with a small icon QR code in the middle) introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time!