*************************具體方法***************************
- (UIImage *)generateQRCode:(NSString *)code width:(CGFloat)width height:(CGFloat)height {
// 生成條形碼圖片
CIImage *qrcodeImage;
NSData *data = [code dataUsingEncoding:NSUTF8StringEncodingallowLossyConversion:false];
/*dataUsingEncoding可以替換為其他形式,其枚舉為:
typedef NSUInteger NSStringEncoding;
NS_ENUM(NSStringEncoding) {
NSASCIIStringEncoding = 1, /* 0..127 only */
NSNEXTSTEPStringEncoding = 2,
NSJapaneseEUCStringEncoding = 3,
NSUTF8StringEncoding = 4,
NSISOLatin1StringEncoding = 5,
NSSymbolStringEncoding = 6,
NSNonLossyASCIIStringEncoding = 7,
NSShiftJISStringEncoding = 8,? ? ? ? ? /* kCFStringEncodingDOSJapanese */
NSISOLatin2StringEncoding = 9,
NSUnicodeStringEncoding = 10,
NSWindowsCP1251StringEncoding = 11,? ? /* Cyrillic; same as AdobeStandardCyrillic */
NSWindowsCP1252StringEncoding = 12,? ? /* WinLatin1 */
NSWindowsCP1253StringEncoding = 13,? ? /* Greek */
NSWindowsCP1254StringEncoding = 14,? ? /* Turkish */
NSWindowsCP1250StringEncoding = 15,? ? /* WinLatin2 */
NSISO2022JPStringEncoding = 21,? ? ? ? /* ISO 2022 Japanese encoding for e-mail */
NSMacOSRomanStringEncoding = 30,
NSUTF16StringEncoding = NSUnicodeStringEncoding,? ? ? /* An alias for NSUnicodeStringEncoding */
NSUTF16BigEndianStringEncoding = 0x90000100,? ? ? ? ? /* NSUTF16StringEncoding encoding with explicit endianness specified */
NSUTF16LittleEndianStringEncoding = 0x94000100,? ? ? /* NSUTF16StringEncoding encoding with explicit endianness specified */
NSUTF32StringEncoding = 0x8c000100,
NSUTF32BigEndianStringEncoding = 0x98000100,? ? ? ? ? /* NSUTF32StringEncoding encoding with explicit endianness specified */
NSUTF32LittleEndianStringEncoding = 0x9c000100? ? ? ? /* NSUTF32StringEncoding encoding with explicit endianness specified */
};
二維碼生成時(shí)腹纳,此處也可以替換*/
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];
}
- (UIImage *)generateBarCode:(NSString *)code width:(CGFloat)width height:(CGFloat)height {
// 生成二維碼圖片
CIImage *barcodeImage;
NSData *data = [code dataUsingEncoding:NSUTF8StringEncoding 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];
}
**********************使用方法******************************
UIImageView *txmImage=[[UIImageView alloc]init];
txmImage.frame=CGRectMake(10, 10, WIDTH-20, 60);
txmImage.image=[self generateBarCode:@"tiaoxingma" width:txmImage.frame.size.width height:txmImage.frame.size.height];
[self.view addSubview:txmImage];
UIImageView *ewmImage=[[UIImageView alloc]init];
ewmImage.frame=CGRectMake(WIDTH/4, 80, WIDTH/2, WIDTH/2);
ewmImage.image=[self generateQRCode:@"erweima" width:ewmImage.frame.size.width height:ewmImage.frame.size.height];
[self.view addSubview:ewmImage];