代碼基本很固定吠冤,做個(gè)筆記浑彰,留著以后需要。
-
生成條形碼
/**
* 生成條形碼
*
* @return 生成條形碼的UIImage對(duì)象
*/
+ (UIImage *)resizeCodeWithString:(NSString *)text BCSize:(CGSize)size
{
CIImage *image = [self generateBarCodeImage:text];
return [self resizeCodeImage:image withSize:size];
}
+ (CIImage *)generateBarCodeImage:(NSString *)source
{
// iOS 8.0以上的系統(tǒng)才支持條形碼的生成拯辙,iOS8.0以下使用第三方控件生成
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
// 注意生成條形碼的編碼方式
NSData *data = [source dataUsingEncoding: NSASCIIStringEncoding];
CIFilter *filter = [CIFilter filterWithName:@"CICode128BarcodeGenerator"];
[filter setValue:data forKey:@"inputMessage"];
// 設(shè)置生成的條形碼的上郭变,下,左涯保,右的margins的值
[filter setValue:[NSNumber numberWithInteger:0] forKey:@"inputQuietSpace"];
return filter.outputImage;
}else{
return nil;
}
}
+ (UIImage *)resizeCodeImage:(CIImage *)image withSize:(CGSize)size
{
if (image) {
CGRect extent = CGRectIntegral(image.extent);
CGFloat scaleWidth = size.width/CGRectGetWidth(extent);
CGFloat scaleHeight = size.height/CGRectGetHeight(extent);
size_t width = CGRectGetWidth(extent) * scaleWidth;
size_t height = CGRectGetHeight(extent) * scaleHeight;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();
CGContextRef contentRef = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpaceRef, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef imageRef = [context createCGImage:image fromRect:extent];
CGContextSetInterpolationQuality(contentRef, kCGInterpolationNone);
CGContextScaleCTM(contentRef, scaleWidth, scaleHeight);
CGContextDrawImage(contentRef, extent, imageRef);
CGImageRef imageRefResized = CGBitmapContextCreateImage(contentRef);
UIImage *barImage = [UIImage imageWithCGImage:imageRefResized];
//Core Foundation 框架下內(nèi)存泄露問(wèn)題诉濒。
CGContextRelease(contentRef);
CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(imageRef);
CGImageRelease(imageRefResized);
return barImage;
}else{
return nil;
}
}
-
生成二維碼
+ (UIImage*)createQRWithString:(NSString*)text QRSize:(CGSize)size;
{
NSData *stringData = [text dataUsingEncoding: NSUTF8StringEncoding];
//生成
CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[qrFilter setValue:stringData forKey:@"inputMessage"];
[qrFilter setValue:@"H" forKey:@"inputCorrectionLevel"];
//上色
CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor"
keysAndValues:
@"inputImage",qrFilter.outputImage,
@"inputColor0",[CIColor colorWithCGColor:[UIColor blackColor].CGColor],
@"inputColor1",[CIColor colorWithCGColor:[UIColor whiteColor].CGColor],
nil];
CIImage *qrImage = colorFilter.outputImage;
//繪制
CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:qrImage fromRect:qrImage.extent];
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRelease(cgImage);
return codeImage;
}
最后
附上掃描的三方庫(kù): LBXScan