iOS開發-產生二維碼圖片【附中間帶有小表徵圖二維碼】(QRCode),iosqrcode
產生二維碼圖片也是項目中常用到的,二維碼的掃描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];
}