1.建立一個(gè)公用類YDQRCode管理二維碼相關(guān)的方法
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface YDQRCode : NSObject
2.在.h文件中定義各個(gè)方法
=====獲取輸出的二維碼
/**
*根據(jù)鏈接印屁,獲取輸出的二維碼
*/
+ (CIImage *)createQRForString:(NSString *)qrString ;
=====根據(jù)CIImage生成指定大小的UIImage
/**
根據(jù)CIImage生成指定大小的UIImage
@param image CIImage
@param size 圖片寬度
*/
+(UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size;
=====產(chǎn)生其它顏色的二維碼圖片
/**
默認(rèn)產(chǎn)生的黑白色的二維碼圖片示惊;我們可以讓它產(chǎn)生其它顏色的二維碼圖片泛范,
例如:藍(lán)白色的二維碼圖片
*/
+(UIImage *)specialColorImage:(UIImage*)image withRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue;
=====合并二維碼圖片和用于中間顯示的圖標(biāo)圖片
/**
使用核心繪圖框架CG(Core Graphics)對(duì)象操作,合并二維碼圖片和用于中間顯示的圖標(biāo)圖片
*/
+(UIImage *)addIconToQRCodeImage:(UIImage *)image withIcon:(UIImage *)icon withIconSize:(CGSize)iconSize;
=====合并二維碼圖片和用于中間顯示的圖標(biāo)圖片
/**
使用核心繪圖框架CG(Core Graphics)對(duì)象操作,
合并二維碼圖片和用于中間顯示的圖標(biāo)圖片
*/
+(UIImage *)addIconToQRCodeImage:(UIImage *)image withIcon:(UIImage *)icon withScale:(CGFloat)scale;
3.在.m文件中實(shí)現(xiàn)具體的方法:
#pragma mark - Private Methods
void ProviderReleaseData (void *info, const void *data, size_t size) {
free((void*)data);
}
#pragma mark - Public Methods
+ (CIImage *) createQRForString:(NSString *)qrString {
//創(chuàng)建過濾器,使用CIFilter濾鏡類生成二維碼
CIFilter * filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
//恢復(fù)默認(rèn)
[filter setDefaults];
//給過濾器添加數(shù)據(jù)(正則表達(dá)式/賬號(hào)和密碼)
NSData * data = [qrString dataUsingEncoding:NSUTF8StringEncoding];
// 設(shè)置內(nèi)容和糾錯(cuò)級(jí)別
[filter setValue:data forKey:@"inputMessage"];
[filter setValue:@"M" forKey:@"inputCorrectionLevel"];
// 返回CIImage
return filter.outputImage;
}
+ (UIImage *) createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
//1.創(chuàng)建bitmap
size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
//創(chuàng)建一個(gè)DeviceGray顏色空間
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef bitmapImage = [context createCGImage:image 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];
}
+ (UIImage *) specialColorImage:(UIImage*)image withRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue {
const int imageWidth = image.size.width;
const int imageHeight = image.size.height;
size_t bytesPerRow = imageWidth * 4;
uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);
//Create context
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGContextRef contextRef = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpaceRef, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CGContextDrawImage(contextRef, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage);
//Traverse pixe
int pixelNum = imageWidth * imageHeight;
uint32_t* pCurPtr = rgbImageBuf;
for (int i = 0; i < pixelNum; i++, pCurPtr++) {
if ((*pCurPtr & 0xFFFFFF00) < 0x99999900) {
//Change color
uint8_t* ptr = (uint8_t*)pCurPtr;
ptr[3] = red; //0~255
ptr[2] = green;
ptr[1] = blue;
} else {
uint8_t* ptr = (uint8_t*)pCurPtr;
ptr[0] = 0;
}
}
//Convert to image
CGDataProviderRef dataProviderRef = CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, ProviderReleaseData);
CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, 8, 32, bytesPerRow, colorSpaceRef,
kCGImageAlphaLast | kCGBitmapByteOrder32Little, dataProviderRef,
NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease(dataProviderRef);
UIImage* img = [UIImage imageWithCGImage:imageRef];
//Release
CGImageRelease(imageRef);
CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpaceRef);
return img;
}
+ (UIImage *) addIconToQRCodeImage:(UIImage *)image withIcon:(UIImage *)icon withIconSize:(CGSize)iconSize {
UIGraphicsBeginImageContext(image.size);
//通過兩張圖片進(jìn)行位置和大小的繪制,實(shí)現(xiàn)兩張圖片的合并;其實(shí)此原理做法也可以用于多張圖片的合并
CGFloat widthOfImage = image.size.width;
CGFloat heightOfImage = image.size.height;
CGFloat widthOfIcon = iconSize.width;
CGFloat heightOfIcon = iconSize.height;
[image drawInRect:CGRectMake(0, 0, widthOfImage, heightOfImage)];
[icon drawInRect:CGRectMake((widthOfImage-widthOfIcon)/2, (heightOfImage-heightOfIcon)/2,
widthOfIcon, heightOfIcon)];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
+ (UIImage *) addIconToQRCodeImage:(UIImage *)image withIcon:(UIImage *)icon withScale:(CGFloat)scale {
UIGraphicsBeginImageContext(image.size);
//通過兩張圖片進(jìn)行位置和大小的繪制早像,實(shí)現(xiàn)兩張圖片的合并;其實(shí)此原理做法也可以用于多張圖片的合并
CGFloat widthOfImage = image.size.width;
CGFloat heightOfImage = image.size.height;
CGFloat widthOfIcon = widthOfImage/scale;
CGFloat heightOfIcon = heightOfImage/scale;
[image drawInRect:CGRectMake(0, 0, widthOfImage, heightOfImage)];
[icon drawInRect:CGRectMake((widthOfImage-widthOfIcon)/2, (heightOfImage-heightOfIcon)/2,
widthOfIcon, heightOfIcon)];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}