- (void)viewDidLoad { //原生條形碼 _barCodeImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 100, 300, 128)]; _barCodeImageView.center=CGPointMake(ScreenWidth/2.0, 100+64); [self.view addSubview:_barCodeImageView];
_barCodeImageView.image=[self generateBarCode:@"1234948958096" width:self.barCodeImageView.frame.size.width height:self.barCodeImageView.frame.size.height]; } -(UIImage*)generateBarCode:(NSString*)barCodeStr width:(CGFloat)width height:(CGFloat)height { // 生成二維碼圖片 CIImage *barcodeImage; NSData *data = [barCodeStr 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]; } |
|