iOS(OC)用系統方法生成二維碼,本文章只是生成
匯入標頭檔案 #import <CoreImage/CoreImage.h>
//建立名為"CIQRCodeGenerator"的CIFilter
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
//將filter所有屬性設定為預設值
[filter setDefaults];
//將所需盡心轉為UTF8的資料,並設定給filter
NSString * str = self.urlTextF.text.length>0?self.urlTextF.text:@"這是一個測試二維碼";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
[filter setValue:data forKey:@"inputMessage"];
//設定二維碼的糾錯水平,越高糾錯水平越高,可以汙損的範圍越大
/*
* L: 7%
* M: 15%
* Q: 25%
* H: 30%
*/
[filter setValue:@"H" forKey:@"inputCorrectionLevel"];
//拿到二維碼圖片,此時的圖片不是很清晰,需要二次加工
CIImage *outPutImage = [filter outputImage];
// UIImage * codeImg = [self sencond_getHDImgWithCIImage:outPutImage
// size:CGSizeMake(130, 130)
// pointColor:nil
// backColor:nil];
UIImage * codeImg = [self getHDImgWithCIImage:outPutImage
size:CGSizeMake(130, 130)
waterImg:[UIImage imageNamed:@"login_logo"]
waterSize:CGSizeMake(35, 35)];
[self.codeImgV setImage:codeImg];
複製程式碼
這個時候已經生成了二維碼,然後需要對其進行大小,顏色,log的編輯
調整圖片大小,顏色
/**
調整二維碼清晰度
@param img 二維碼圖片
@param size 二維碼大小
@param pointColor 二維碼的顏色(預設黑色)
@param backgroundColor 二維碼的背景(預設白色)
@return 生成後的二維碼
*/
- (UIImage *)sencond_getHDImgWithCIImage:(CIImage *)img size:(CGSize)size pointColor:(UIColor *)pointColor backColor:(UIColor *)backgroundColor{
//設定二維碼的顏色
if (pointColor == nil) {
pointColor = [UIColor blackColor];
}
//設定背景顏色
if (backgroundColor == nil) {
backgroundColor = [UIColor whiteColor];
}
CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor"
keysAndValues:
@"inputImage", img,
@"inputColor0", [CIColor colorWithCGColor:pointColor.CGColor],
@"inputColor1", [CIColor colorWithCGColor:backgroundColor.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;
}
複製程式碼
新增水印log圖片
/**
調整二維碼清晰度,新增水印圖片
@param img 模糊的二維碼圖片
@param size 需要的二維碼寬高
@param waterImg log圖片
@param waterSize log圖片的大小
@return 新增log後的二維碼
*/
- (UIImage *)getHDImgWithCIImage:(CIImage *)img size:(CGSize)size waterImg:(UIImage *)waterImg waterSize:(CGSize)waterSize {
CGRect extent = CGRectIntegral(img.extent);
CGFloat scale = MIN(size.width/CGRectGetWidth(extent), size.height/CGRectGetHeight(extent));
//1.建立bitmap;
size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
//建立一個DeviceGray顏色空間
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
//width:圖片寬度畫素
//height:圖片高度畫素
//bitsPerComponent:每個顏色的位元值,例如在rgba-32模式下為8
//bitmapInfo:指定的點陣圖應該包含一個alpha通道。
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
//建立CoreGraphics image
CGImageRef bitmapImage = [context createCGImage:img fromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
//2.儲存bitmap到圖片
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef); CGImageRelease(bitmapImage);
//清晰的二維碼圖片
UIImage *outputImage = [UIImage imageWithCGImage:scaledImage];
//給二維碼加 logo 圖
UIGraphicsBeginImageContextWithOptions(outputImage.size, NO, [[UIScreen mainScreen] scale]);
[outputImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
//水印圖片
//把水印圖片畫到生成的二維碼圖片上,注意尺寸不要太大(根據上面生成二維碼設定的糾錯程度設定),否則有可能造成掃不出來
[waterImg drawInRect:CGRectMake((size.width-waterSize.width)/2.0, (size.height-waterSize.height)/2.0, waterSize.width, waterSize.height)];
UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newPic;
}
複製程式碼