ios创建二维码
#import "LCTwoCodeImage.h"
@implementation LCTwoCodeImage
+(UIImage *) GotoCreatMyTwoCode :(NSString *) string {
//通过传入的字符串来创建二维码初始图层渲染
CIImage * firstImage = [self creatCIImageFromString:string];
//通过 CIImage 来生成二维码
UIImage * qrCode = [self createNonInterpolatedUIImageFormCIImage:firstImage withSize:250.0f];
return qrCode;
}
+(UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// create a bitmap image that we'll draw into a bitmap context at the desired size;
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:image fromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
// Create an image with the contents of our bitmap
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
// Cleanup
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}
+(CIImage *)creatCIImageFromString:(NSString *) string {
NSData * stringData = [string dataUsingEncoding:NSUTF8StringEncoding];
//添加图层
CIFilter * qrFiler = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[qrFiler setValue:stringData forKey:@"inputMessage"];
[qrFiler setValue:@"M" forKey:@"inputCorrectionLevel"];
return [qrFiler outputImage];
}
@end