- (void)viewDidLoad {
[super viewDidLoad];
self.imageView.image = [self generateBarCode:@"1524829417" 60 height:60];
self.imageView2.image = [self generateQRCode:@"1524829417" 60 height:60];
}
- (UIImage *)generateBarCode:(NSString *)code (CGFloat)width height:(CGFloat)height {
// 生成条形码图片
CIImage *barcodeImage;
NSData *data = [code dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:false];
CIFilter *filter = [CIFilter filterWithName:@"CICode128BarcodeGenerator"];
[filter setValue:data forKey:@"inputMessage"];
barcodeImage = [filter outputImage];
// 消除模糊
CGFloat scaleX = width / barcodeImage.extent.size.width; // extent 返回图片的frame
CGFloat scaleY = height / barcodeImage.extent.size.height;
CIImage *transformedImage = [barcodeImage imageByApplyingTransform:CGAffineTransformScale(CGAffineTransformIdentity, scaleX, scaleY)];
return [UIImage imageWithCIImage:transformedImage];
}
- (UIImage *)generateQRCode:(NSString *)code (CGFloat)width height:(CGFloat)height {
// 生成二维码图片
CIImage *qrcodeImage;
NSData *data = [code dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:false];
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setValue:data forKey:@"inputMessage"];
[filter setValue:@"H" forKey:@"inputCorrectionLevel"];
qrcodeImage = [filter outputImage];
// 消除模糊
CGFloat scaleX = width / qrcodeImage.extent.size.width; // extent 返回图片的frame
CGFloat scaleY = height / qrcodeImage.extent.size.height;
CIImage *transformedImage = [qrcodeImage imageByApplyingTransform:CGAffineTransformScale(CGAffineTransformIdentity, scaleX, scaleY)];
return [UIImage imageWithCIImage:transformedImage];
}