二维码图片生成(扩展知识:创建带圆角效果的图片)

效果如下:

二维码图片生成(扩展知识:创建带圆角效果的图片)

ViewController.h

1 #import <UIKit/UIKit.h>

2 @interface ViewController : UIViewController

3 @property (strong, nonatomic) IBOutlet UIImageView *imgVQRCode;

4 

5 @end

ViewController.m

 1 #import "ViewController.h"

 2 #import "KMQRCode.h"

 3 #import "UIImage+RoundedRectImage.h"

 4 

 5 @interface ViewController ()

 6 - (void)layoutUI;

 7 @end

 8 

 9 @implementation ViewController

10 

11 - (void)viewDidLoad {

12     [super viewDidLoad];

13     

14     [self layoutUI];

15 }

16 

17 - (void)didReceiveMemoryWarning {

18     [super didReceiveMemoryWarning];

19     // Dispose of any resources that can be recreated.

20 }

21 

22 - (void)layoutUI {

23     //用于生成二维码的字符串source

24     NSString *source = @"https://github.com/KenmuHuang";

25     

26     //使用iOS 7后的CIFilter对象操作,生成二维码图片imgQRCode(会拉伸图片,比较模糊,效果不佳)

27     CIImage *imgQRCode = [KMQRCode createQRCodeImage:source];

28     

29     //使用核心绘图框架CG(Core Graphics)对象操作,进一步针对大小生成二维码图片imgAdaptiveQRCode(图片大小适合,清晰,效果好)

30     UIImage *imgAdaptiveQRCode = [KMQRCode resizeQRCodeImage:imgQRCode

31                                                     withSize:_imgVQRCode.frame.size.width];

32     

33     //默认产生的黑白色的二维码图片;我们可以让它产生其它颜色的二维码图片,例如:蓝白色的二维码图片

34     imgAdaptiveQRCode = [KMQRCode specialColorImage:imgAdaptiveQRCode

35                                             withRed:33.0

36                                               green:114.0

37                                                blue:237.0]; //0~255

38     

39     //使用核心绘图框架CG(Core Graphics)对象操作,创建带圆角效果的图片

40     UIImage *imgIcon = [UIImage createRoundedRectImage:[UIImage imageNamed:@"QRCode"]

41                                               withSize:CGSizeMake(70.0, 93.0)

42                                             withRadius:10];

43     //使用核心绘图框架CG(Core Graphics)对象操作,合并二维码图片和用于中间显示的图标图片

44     imgAdaptiveQRCode = [KMQRCode addIconToQRCodeImage:imgAdaptiveQRCode

45                                               withIcon:imgIcon

46                                           withIconSize:imgIcon.size];

47     

48 //    imgAdaptiveQRCode = [KMQRCode addIconToQRCodeImage:imgAdaptiveQRCode

49 //                                              withIcon:imgIcon

50 //                                             withScale:3];

51     

52     _imgVQRCode.image = imgAdaptiveQRCode;

53     //设置图片视图的圆角边框效果

54     _imgVQRCode.layer.masksToBounds = YES;

55     _imgVQRCode.layer.cornerRadius = 10.0;

56     _imgVQRCode.layer.borderColor = [UIColor lightGrayColor].CGColor;

57     _imgVQRCode.layer.borderWidth = 4.0;

58 }

59 

60 @end

​UIImage+RoundedRectImage.h

1 #import <UIKit/UIKit.h>

2 

3 @interface UIImage (RoundedRectImage)

4 + (UIImage *)createRoundedRectImage:(UIImage *)image withSize:(CGSize)size withRadius:(NSInteger)radius;

5 

6 @end

UIImage+RoundedRectImage.m

 1 #import "UIImage+RoundedRectImage.h"

 2 

 3 @implementation UIImage (RoundedRectImage)

 4 

 5 #pragma mark - Private Methods

 6 static void addRoundedRectToPath(CGContextRef contextRef, CGRect rect, float widthOfRadius, float heightOfRadius) {

 7     float fw, fh;

 8     if (widthOfRadius == 0 || heightOfRadius == 0)

 9     {

10         CGContextAddRect(contextRef, rect);

11         return;

12     }

13     

14     CGContextSaveGState(contextRef);

15     CGContextTranslateCTM(contextRef, CGRectGetMinX(rect), CGRectGetMinY(rect));

16     CGContextScaleCTM(contextRef, widthOfRadius, heightOfRadius);

17     fw = CGRectGetWidth(rect) / widthOfRadius;

18     fh = CGRectGetHeight(rect) / heightOfRadius;

19     

20     CGContextMoveToPoint(contextRef, fw, fh/2);  // Start at lower right corner

21     CGContextAddArcToPoint(contextRef, fw, fh, fw/2, fh, 1);  // Top right corner

22     CGContextAddArcToPoint(contextRef, 0, fh, 0, fh/2, 1); // Top left corner

23     CGContextAddArcToPoint(contextRef, 0, 0, fw/2, 0, 1); // Lower left corner

24     CGContextAddArcToPoint(contextRef, fw, 0, fw, fh/2, 1); // Back to lower right

25     

26     CGContextClosePath(contextRef);

27     CGContextRestoreGState(contextRef);

28 }

29 

30 #pragma mark - Public Methods

31 + (UIImage *)createRoundedRectImage:(UIImage *)image withSize:(CGSize)size withRadius:(NSInteger)radius {

32     int w = size.width;

33     int h = size.height;

34     

35     CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();

36     CGContextRef contextRef = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpaceRef, (CGBitmapInfo)kCGImageAlphaPremultipliedFirst);

37     CGRect rect = CGRectMake(0, 0, w, h);

38     

39     CGContextBeginPath(contextRef);

40     addRoundedRectToPath(contextRef, rect, radius, radius);

41     CGContextClosePath(contextRef);

42     CGContextClip(contextRef);

43     CGContextDrawImage(contextRef, CGRectMake(0, 0, w, h), image.CGImage);

44     CGImageRef imageMasked = CGBitmapContextCreateImage(contextRef);

45     UIImage *img = [UIImage imageWithCGImage:imageMasked];

46     

47     CGContextRelease(contextRef);

48     CGColorSpaceRelease(colorSpaceRef);

49     CGImageRelease(imageMasked);

50     return img;

51 }

52 

53 @end

KMQRCode.h

 1 #import <UIKit/UIKit.h>

 2 

 3 @interface KMQRCode : NSObject

 4 + (CIImage *)createQRCodeImage:(NSString *)source;

 5 + (UIImage *)resizeQRCodeImage:(CIImage *)image withSize:(CGFloat)size;

 6 + (UIImage *)specialColorImage:(UIImage*)image withRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue;

 7 + (UIImage *)addIconToQRCodeImage:(UIImage *)image withIcon:(UIImage *)icon withIconSize:(CGSize)iconSize;

 8 + (UIImage *)addIconToQRCodeImage:(UIImage *)image withIcon:(UIImage *)icon withScale:(CGFloat)scale;

 9 

10 @end

KMQRCode.m

  1 #import "KMQRCode.h"

  2 

  3 @implementation KMQRCode

  4 

  5 #pragma mark - Private Methods

  6 void ProviderReleaseData (void *info, const void *data, size_t size){

  7     free((void*)data);

  8 }

  9 

 10 #pragma mark - Public Methods

 11 + (CIImage *)createQRCodeImage:(NSString *)source {

 12     NSData *data = [source dataUsingEncoding:NSUTF8StringEncoding];

 13     

 14     CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];

 15     [filter setValue:data forKey:@"inputMessage"];

 16     [filter setValue:@"Q" forKey:@"inputCorrectionLevel"]; //设置纠错等级越高;即识别越容易,值可设置为L(Low) |  M(Medium) | Q | H(High)

 17     return filter.outputImage;

 18 }

 19 

 20 + (UIImage *)resizeQRCodeImage:(CIImage *)image withSize:(CGFloat)size {

 21     CGRect extent = CGRectIntegral(image.extent);

 22     CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));

 23     size_t width = CGRectGetWidth(extent) * scale;

 24     size_t height = CGRectGetHeight(extent) * scale;

 25     CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();

 26     

 27     CGContextRef contextRef = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpaceRef, (CGBitmapInfo)kCGImageAlphaNone);

 28     CIContext *context = [CIContext contextWithOptions:nil];

 29     CGImageRef imageRef = [context createCGImage:image fromRect:extent];

 30     CGContextSetInterpolationQuality(contextRef, kCGInterpolationNone);

 31     CGContextScaleCTM(contextRef, scale, scale);

 32     CGContextDrawImage(contextRef, extent, imageRef);

 33     

 34     CGImageRef imageRefResized = CGBitmapContextCreateImage(contextRef);

 35     

 36     //Release

 37     CGContextRelease(contextRef);

 38     CGImageRelease(imageRef);

 39     return [UIImage imageWithCGImage:imageRefResized];

 40 }

 41 

 42 + (UIImage *)specialColorImage:(UIImage*)image withRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue {

 43     const int imageWidth = image.size.width;

 44     const int imageHeight = image.size.height;

 45     size_t bytesPerRow = imageWidth * 4;

 46     uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);

 47     

 48     //Create context

 49     CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();

 50     CGContextRef contextRef = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpaceRef, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);

 51     CGContextDrawImage(contextRef, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage);

 52     

 53     //Traverse pixe

 54     int pixelNum = imageWidth * imageHeight;

 55     uint32_t* pCurPtr = rgbImageBuf;

 56     for (int i = 0; i < pixelNum; i++, pCurPtr++){

 57         if ((*pCurPtr & 0xFFFFFF00) < 0x99999900){

 58             //Change color

 59             uint8_t* ptr = (uint8_t*)pCurPtr;

 60             ptr[3] = red; //0~255

 61             ptr[2] = green;

 62             ptr[1] = blue;

 63         }else{

 64             uint8_t* ptr = (uint8_t*)pCurPtr;

 65             ptr[0] = 0;

 66         }

 67     }

 68     

 69     //Convert to image

 70     CGDataProviderRef dataProviderRef = CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, ProviderReleaseData);

 71     CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, 8, 32, bytesPerRow, colorSpaceRef,

 72                                         kCGImageAlphaLast | kCGBitmapByteOrder32Little, dataProviderRef,

 73                                         NULL, true, kCGRenderingIntentDefault);

 74     CGDataProviderRelease(dataProviderRef);

 75     UIImage* img = [UIImage imageWithCGImage:imageRef];

 76     

 77     //Release

 78     CGImageRelease(imageRef);

 79     CGContextRelease(contextRef);

 80     CGColorSpaceRelease(colorSpaceRef);

 81     return img;

 82 }

 83 

 84 +(UIImage *)addIconToQRCodeImage:(UIImage *)image withIcon:(UIImage *)icon withIconSize:(CGSize)iconSize {

 85     UIGraphicsBeginImageContext(image.size);

 86     //通过两张图片进行位置和大小的绘制,实现两张图片的合并;其实此原理做法也可以用于多张图片的合并

 87     CGFloat widthOfImage = image.size.width;

 88     CGFloat heightOfImage = image.size.height;

 89     CGFloat widthOfIcon = iconSize.width;

 90     CGFloat heightOfIcon = iconSize.height;

 91     

 92     [image drawInRect:CGRectMake(0, 0, widthOfImage, heightOfImage)];

 93     [icon drawInRect:CGRectMake((widthOfImage-widthOfIcon)/2, (heightOfImage-heightOfIcon)/2,

 94                                 widthOfIcon, heightOfIcon)];

 95     UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

 96     

 97     UIGraphicsEndImageContext();

 98     return img;

 99 }

100 

101 +(UIImage *)addIconToQRCodeImage:(UIImage *)image withIcon:(UIImage *)icon withScale:(CGFloat)scale {

102     UIGraphicsBeginImageContext(image.size);

103     

104     //通过两张图片进行位置和大小的绘制,实现两张图片的合并;其实此原理做法也可以用于多张图片的合并

105     CGFloat widthOfImage = image.size.width;

106     CGFloat heightOfImage = image.size.height;

107     CGFloat widthOfIcon = widthOfImage/scale;

108     CGFloat heightOfIcon = heightOfImage/scale;

109     

110     [image drawInRect:CGRectMake(0, 0, widthOfImage, heightOfImage)];

111     [icon drawInRect:CGRectMake((widthOfImage-widthOfIcon)/2, (heightOfImage-heightOfIcon)/2,

112                                 widthOfIcon, heightOfIcon)];

113     UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

114     

115     UIGraphicsEndImageContext();

116     return img;

117 }

118 

119 @end

 

你可能感兴趣的:(二维码)