OC版二维码生成
// 输入要转换的string与二维码尺寸
+ (UIImage *)createQRForString:(NSString *)qrString andSize:(CGFloat)size {
NSData *stringData = [qrString dataUsingEncoding:NSUTF8StringEncoding];
// 创建filter
CIFilter *qrFilter = [CIFilter filterWithName:@”CIQRCodeGenerator”];
// 设置内容和纠错级别
[qrFilter setValue:stringData forKey:@”inputMessage”];
[qrFilter setValue:@”H” forKey:@”inputCorrectionLevel”];
// 处理生成的CIImage
CGFloat scaleX = size / CGRectGetWidth(qrFilter.outputImage.extent);
CGFloat scaleY = size / CGRectGetHeight(qrFilter.outputImage.extent);
CIImage *transformImage = [qrcodeImage imageByApplyingTransform:CGAffineTransformScale(CGAffineTransformIdentity, scaleX, scaleY];
return [UIImage imageWithCIImage:transformImage];
}
swift版二维码生成
// 输入要转换的string与二维码尺寸
class func creatQRCode(qrString:String!,size:CGFloat!) -> UIImage {
let stringData = qrString.dataUsingEncoding(NSUTF8StringEncoding)
//创建filter
let qrFilter = CIFilter(name: “CIQRCodeGenerator”)
//设置内容和纠错级别
qrFilter?.setValue(stringData, forKey: “inputMessage”)
qrFilter?.setValue(“H”, forKey: “inputCorrectionLevel”)
// 处理生成的CIImage
let x = size/CGRectGetWidth((qrFilter?.outputImage?.extent)!)
let y = size/CGRectGetHeight((qrFilter?.outputImage?.extent)!)
let ciImage = qrFilter?.outputImage?.imageByApplyingTransform(CGAffineTransformScale(CGAffineTransformIdentity, x, y))
return UIImage(CIImage: ciImage!)
}