@implementation UIImage (QR) + (UIImage *)qrImageForString:(NSString *)string imageSize:(CGFloat)Imagesize { CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; [filter setDefaults]; NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; [filter setValue:data forKey:@"inputMessage"]; [filter setValue:@"H" forKey:@"inputCorrectionLevel"]; CIImage *outPutImage = [filter outputImage]; // CGAffineTransform transform = CGAffineTransformMakeScale(5, 5); // outPutImage = [outPutImage imageByApplyingTransform:transform]; UIImage *image = [UIImage imageWithCIImage:outPutImage]; return image; } @end 默认生成是100X100的图片.如果要放大,取消注释代码 Generates a Quick Response code (two-dimensional barcode) from input data. Localized Display Name CIQRCodeGenerator Parameters inputMessage The data to be encoded as a QR code. An NSData object whose display name is Message. inputCorrectionLevel A single letter specifying the error correction format. An NSString object whose display name is CorrectionLevel.Default value: M Discussion Generates an output image representing the input data according to the ISO/IEC 18004:2006 standard. The width and height of each module (square dot) of the code in the output image is one point. To create a QR code from a string or URL, convert it to an NSData object using the NSISOLatin1StringEncoding string encoding. The inputCorrectionLevel parameter controls the amount of additional data encoded in the output image to provide error correction. Higher levels of error correction result in larger output images but allow larger areas of the code to be damaged or obscured without. There are four possible correction modes (with corresponding error resilience levels): L: 7% M: 15% Q: 25% H: 30%
Availability
Available in OS X v10.9 and later and in iOS 7.0 and later.