效果图:
图1 点击图中按钮 生成二维码 图2 生成二维码
核心代码:
- (UIImage *)createQRCodeOrBarCode:(BOOL)QROrBar code:(NSString *)code width:(CGFloat)width height:(CGFloat)height {
if (!code || code.length == 0) {
//code为nil, 不能生成二维码
//code为@“”,可生成二维码,但扫描结果为:(null),无意义
return nil;
}
CIImage *returnImage;
NSData *data = [code dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:NO];
CIFilter *filter;
if (QROrBar) {
// 生成条形码
//创建滤镜,对图像进行滤镜处理
filter = [CIFilter filterWithName:@"CICode128BarcodeGenerator"];
[filter setValue:data forKey:@"inputMessage"];
returnImage = [filter outputImage];
} else {
// 生成二维码
filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setValue:data forKey:@"inputMessage"];
[filter setValue:@"H" forKey:@"inputCorrectionLevel"];
returnImage = [filter outputImage];
}
//1.保持模糊效果
return [UIImage imageWithCIImage:returnImage];
/**
// 2.消除模糊, extent 返回图片的frame
CGFloat scaleX = width / returnImage.extent.size.width;
CGFloat scaleY = height / returnImage.extent.size.height;
CIImage *transformedImage = [returnImage imageByApplyingTransform:CGAffineTransformScale(CGAffineTransformIdentity, scaleX, scaleY)];
return [UIImage imageWithCIImage:transformedImage];
*/
}
测试:
图3 按钮点击事件 图4 关联imageView属性
网友评论