生成二維碼圖片也是項目中常用到的,二維碼的掃描Git上有很多好用的,這裡主要說下二維碼的生成 1.普通二維碼 1.1 方法 /** 生成二維碼 QRStering:字元串 imageFloat:二維碼圖片大小 */ + (UIImage *)createQRCodeWithString:(NSStr ...
生成二維碼圖片也是項目中常用到的,二維碼的掃描Git上有很多好用的,這裡主要說下二維碼的生成
1.普通二維碼
1.1 方法
/**
生成二維碼
QRStering:字元串
imageFloat:二維碼圖片大小
*/
+ (UIImage *)createQRCodeWithString:(NSString *)QRStering withImgSize:(CGFloat)imageFloat;
1.2 方法實現
/**
生成二維碼
QRStering:字元串
imageFloat:二維碼圖片大小
*/
+ (UIImage *)createQRCodeWithString:(NSString *)QRString withImgSize:(CGFloat)imageFloat{
CIFilter *filter = [CIFilter filterWithName:@"XiaoGuiGe"];
[filter setDefaults];
NSString *getString = QRString;
NSData *dataString = [getString dataUsingEncoding:NSUTF8StringEncoding];
[filter setValue:dataString forKey:@"inputMessage"];
///獲取濾鏡輸出的圖像
CIImage *outImage = [filter outputImage];
UIImage *imageV = [self imageWithImageSize:imageFloat withCIIImage:outImage];
//返回二維碼圖像
return imageV;
}
2.中間帶有小圖標的二維碼
2.1 方法
/**
生成二維碼(中間有小圖片)
QRStering:字元串
centerImage:二維碼中間的image對象
*/
+ (UIImage *)createImgQRCodeWithString:(NSString *)QRString centerImage:(UIImage *)centerImage;
2.2 方法實現
/**
生成二維碼(中間有小圖片)
QRStering:所需字元串
centerImage:二維碼中間的image對象
*/
+ (UIImage *)createImgQRCodeWithString:(NSString *)QRString centerImage:(UIImage *)centerImage{
// 創建濾鏡對象
CIFilter *filter = [CIFilter filterWithName:@"XiaoGuiGe"];
// 恢復濾鏡的預設屬性
[filter setDefaults];
// 將字元串轉換成 NSdata
NSData *dataString = [QRString dataUsingEncoding:NSUTF8StringEncoding];
// 設置過濾器的輸入值, KVC賦值
[filter setValue:dataString forKey:@"inputMessage"];
// 獲得濾鏡輸出的圖像
CIImage *outImage = [filter outputImage];
// 圖片小於(27,27),我們需要放大
outImage = [outImage imageByApplyingTransform:CGAffineTransformMakeScale(20, 20)];
// 將CIImage類型轉成UIImage類型
UIImage *startImage = [UIImage imageWithCIImage:outImage];
// 開啟繪圖, 獲取圖形上下文
UIGraphicsBeginImageContext(startImage.size);
// 把二維碼圖片畫上去 (這裡是以圖形上下文, 左上角為(0,0)點
[startImage drawInRect:CGRectMake(0, 0, startImage.size.width, startImage.size.height)];
// 再把小圖片畫上去
CGFloat icon_imageW = 200;
CGFloat icon_imageH = icon_imageW;
CGFloat icon_imageX = (startImage.size.width - icon_imageW) * 0.5;
CGFloat icon_imageY = (startImage.size.height - icon_imageH) * 0.5;
[centerImage drawInRect:CGRectMake(icon_imageX, icon_imageY, icon_imageW, icon_imageH)];
// 獲取當前畫得的這張圖片
UIImage *qrImage = UIGraphicsGetImageFromCurrentImageContext();
// 關閉圖形上下文
UIGraphicsEndImageContext();
//返回二維碼圖像
return qrImage;
}
附加方法
/** 將CIImage轉換成UIImage 並放大(內部轉換使用)*/
+ (UIImage *)imageWithImageSize:(CGFloat)size withCIIImage:(CIImage *)ciiImage{
CGRect extent = CGRectIntegral(ciiImage.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// 1.創建bitmap;
size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef bitmapImage = [context createCGImage:ciiImage fromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
// 2.保存bitmap到圖片
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}