DYFQRCodeImageView.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // DYFQRCodeImageView.m
  3. //
  4. // Created by dyf on 2018/01/28.
  5. // Copyright © 2018 dyf. All rights reserved.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. #import "DYFQRCodeImageView.h"
  26. #import "DYFCodeScannerMacros.h"
  27. @implementation DYFQRCodeImageView
  28. + (instancetype)createWithFrame:(CGRect)frame stringValue:(NSString *)stringValue {
  29. return [self createWithFrame:frame stringValue:stringValue centerImage:nil];
  30. }
  31. + (instancetype)createWithFrame:(CGRect)frame stringValue:(NSString *)stringValue centerImage:(UIImage *)centerImage {
  32. return [[self alloc] initWithFrame:frame stringValue:stringValue centerImage:centerImage];
  33. }
  34. + (instancetype)createWithFrame:(CGRect)frame stringValue:(NSString *)stringValue backgroudColor:(UIColor *)backgroudColor foregroudColor:(UIColor *)foregroudColor {
  35. return [self createWithFrame:frame stringValue:stringValue backgroudColor:backgroudColor foregroudColor:foregroudColor centerImage:nil];
  36. }
  37. + (instancetype)createWithFrame:(CGRect)frame stringValue:(NSString *)stringValue backgroudColor:(UIColor *)backgroudColor foregroudColor:(UIColor *)foregroudColor centerImage:(UIImage *)centerImage {
  38. return [[self alloc] initWithFrame:frame stringValue:stringValue backgroudColor:backgroudColor foregroudColor:foregroudColor centerImage:centerImage];
  39. }
  40. - (instancetype)initWithFrame:(CGRect)frame stringValue:(NSString *)stringValue {
  41. return [self initWithFrame:frame stringValue:stringValue centerImage:nil];
  42. }
  43. - (instancetype)initWithFrame:(CGRect)frame stringValue:(NSString *)stringValue centerImage:(UIImage *)centerImage {
  44. self = [super initWithFrame:frame];
  45. if (self) {
  46. [self generateQRCodeWithStringValue:stringValue centerImage:centerImage];
  47. }
  48. return self;
  49. }
  50. - (instancetype)initWithFrame:(CGRect)frame stringValue:(NSString *)stringValue backgroudColor:(UIColor *)backgroudColor foregroudColor:(UIColor *)foregroudColor {
  51. return [self initWithFrame:frame stringValue:stringValue backgroudColor:backgroudColor foregroudColor:foregroudColor centerImage:nil];
  52. }
  53. - (instancetype)initWithFrame:(CGRect)frame stringValue:(NSString *)stringValue backgroudColor:(UIColor *)backgroudColor foregroudColor:(UIColor *)foregroudColor centerImage:(UIImage *)centerImage {
  54. self = [super initWithFrame:frame];
  55. if (self) {
  56. [self generateQRCodeWithStringValue:stringValue backgroudColor:backgroudColor foregroudColor:foregroudColor centerImage:centerImage];
  57. }
  58. return self;
  59. }
  60. - (void)generateQRCodeWithStringValue:(NSString *)stringValue centerImage:(UIImage *)centerImage {
  61. // 创建二维码过滤器
  62. CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
  63. [qrFilter setDefaults];
  64. // 生成二维码
  65. NSData *data = [stringValue dataUsingEncoding:NSUTF8StringEncoding];
  66. [qrFilter setValue:data forKey:@"inputMessage"];
  67. CIImage *codeImage = qrFilter.outputImage;
  68. // 拉伸
  69. codeImage = [codeImage imageByApplyingTransform:CGAffineTransformMakeScale(9, 9)];
  70. // 绘制二维码
  71. UIImage *qrImage = [UIImage imageWithCIImage:codeImage];
  72. UIGraphicsBeginImageContext(qrImage.size);
  73. [qrImage drawInRect:CGRectMake(0, 0, qrImage.size.width, qrImage.size.height)];
  74. if (centerImage) {
  75. CGFloat meImgX = (qrImage.size.width - DYFQRCodeMeImageW)/2.f;
  76. CGFloat meImgY = (qrImage.size.height - DYFQRCodeMeImageH)/2.f;
  77. [centerImage drawInRect:CGRectMake(meImgX, meImgY, DYFQRCodeMeImageW, DYFQRCodeMeImageH)];
  78. }
  79. // 获取绘制好的二维码
  80. self.image = UIGraphicsGetImageFromCurrentImageContext();
  81. UIGraphicsEndImageContext();
  82. }
  83. - (void)generateQRCodeWithStringValue:(NSString *)stringValue backgroudColor:(UIColor *)backgroudColor foregroudColor:(UIColor *)foregroudColor centerImage:(UIImage *)centerImage {
  84. // 创建二维码过滤器
  85. CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
  86. [qrFilter setDefaults];
  87. // 生成二维码
  88. NSData *data = [stringValue dataUsingEncoding:NSUTF8StringEncoding];
  89. [qrFilter setValue:data forKey:@"inputMessage"];
  90. CIImage *codeImage = qrFilter.outputImage;
  91. // 生成带有颜色的二维码
  92. // 创建颜色过滤器
  93. CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor"];
  94. // 设置默认值
  95. [colorFilter setDefaults];
  96. // 输入内容
  97. // 画颜色的图片
  98. [colorFilter setValue:codeImage forKey:@"inputImage"];
  99. // 使用CIColor
  100. // inputColor0 前景色 [CIColor colorWithRed:1 green:0 blue:0]
  101. [colorFilter setValue:[CIColor colorWithCGColor:foregroudColor.CGColor] forKey:@"inputColor0"];
  102. // inputColor1 背景色 [CIColor colorWithRed:0 green:1 blue:0]
  103. [colorFilter setValue:[CIColor colorWithCGColor:backgroudColor.CGColor] forKey:@"inputColor1"];
  104. codeImage = colorFilter.outputImage;
  105. // 拉伸
  106. codeImage = [codeImage imageByApplyingTransform:CGAffineTransformMakeScale(9, 9)];
  107. // 绘制二维码
  108. UIImage *qrImage = [UIImage imageWithCIImage:codeImage];
  109. UIGraphicsBeginImageContext(qrImage.size);
  110. [qrImage drawInRect:CGRectMake(0, 0, qrImage.size.width, qrImage.size.height)];
  111. if (centerImage) {
  112. CGFloat meImgX = (qrImage.size.width - DYFQRCodeMeImageW)/2.f;
  113. CGFloat meImgY = (qrImage.size.height - DYFQRCodeMeImageH)/2.f;
  114. [centerImage drawInRect:CGRectMake(meImgX, meImgY, DYFQRCodeMeImageW, DYFQRCodeMeImageH)];
  115. }
  116. // 获取绘制好的二维码
  117. self.image = UIGraphicsGetImageFromCurrentImageContext();
  118. UIGraphicsEndImageContext();
  119. }
  120. @end