• 自定义二维码的生成


    用系统的框架写的代码, 可以改变二维码的大小和颜色

      1 #import "ViewController.h"
      2 
      3  
      4 
      5 @interface ViewController ()
      6 
      7 @property (strong, nonatomic) IBOutlet UIImageView *qrcodeView;
      8 
      9 @property (nonatomic, strong) UIImageView *pictureView;
     10 
     11  
     12 
     13 @end
     14 
     15  
     16 
     17 @implementation ViewController
     18 
     19             
     20 
     21 - (void)viewDidLoad {
     22 
     23     [super viewDidLoad];
     24 
     25     // Do any additional setup after loading the view, typically from a nib.
     26 
     27     // 创建二维码图片
     28 
     29     UIImage *qrcode = [self createNonInterpolatedUIImageFormCIImage:[self createQRForString:@"http://www.cnblogs.com/lurenq/p/5338576.html"] withSize:250.0f];
     30 
     31     // 自定义二维码的颜色
     32 
     33     UIImage *customQrcode = [self imageBlackToTransparent:qrcode withRed:0.0f andGreen:0.0f andBlue:255.0f];
     34 
     35     self.qrcodeView.image = customQrcode;
     36 
     37     // set shadow
     38 
     39     self.qrcodeView.layer.shadowOffset = CGSizeMake(0, 2);
     40 
     41     self.qrcodeView.layer.shadowRadius = 2;
     42 
     43     self.qrcodeView.layer.shadowColor = [UIColor blackColor].CGColor;
     44 
     45     self.qrcodeView.layer.shadowOpacity = 0.5;
     46 
     47     
     48 
     49     // 添加标识图片
     50 
     51     self.pictureView = [[UIImageView alloc] init];
     52 
     53     self.pictureView.frame  = CGRectMake(100, 100, 50, 50);
     54 
     55     // icon , 不影响扫描
     56 
     57     self.pictureView.image = [UIImage imageNamed:@"1"];
     58 
     59     [self.qrcodeView addSubview:self.pictureView];
     60 
     61     
     62 
     63     
     64 
     65 }
     66 
     67  
     68 
     69  
     70 
     71 - (void)didReceiveMemoryWarning {
     72 
     73     [super didReceiveMemoryWarning];
     74 
     75     // Dispose of any resources that can be recreated.
     76 
     77 }
     78 
     79  
     80 
     81 #pragma mark - InterpolatedUIImage
     82 
     83 - (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
     84 
     85     CGRect extent = CGRectIntegral(image.extent);
     86 
     87     CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
     88 
     89     // create a bitmap image that we'll draw into a bitmap context at the desired size;
     90 
     91     size_t width = CGRectGetWidth(extent) * scale;
     92 
     93     size_t height = CGRectGetHeight(extent) * scale;
     94 
     95     CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
     96 
     97     CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
     98 
     99     CIContext *context = [CIContext contextWithOptions:nil];
    100 
    101     CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    102 
    103     CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    104 
    105     CGContextScaleCTM(bitmapRef, scale, scale);
    106 
    107     CGContextDrawImage(bitmapRef, extent, bitmapImage);
    108 
    109     // Create an image with the contents of our bitmap
    110 
    111     CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    112 
    113     // Cleanup
    114 
    115     CGContextRelease(bitmapRef);
    116 
    117     CGImageRelease(bitmapImage);
    118 
    119     return [UIImage imageWithCGImage:scaledImage];
    120 
    121 }
    122 
    123  
    124 
    125 #pragma mark - QRCodeGenerator
    126 
    127 - (CIImage *)createQRForString:(NSString *)qrString {
    128 
    129     // Need to convert the string to a UTF-8 encoded NSData object
    130 
    131     NSData *stringData = [qrString dataUsingEncoding:NSUTF8StringEncoding];
    132 
    133     // Create the filter
    134 
    135     CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    136 
    137     // Set the message content and error-correction level
    138 
    139     [qrFilter setValue:stringData forKey:@"inputMessage"];
    140 
    141     [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"];
    142 
    143     // Send the image back
    144 
    145     return qrFilter.outputImage;
    146 
    147 }
    148 
    149  
    150 
    151 #pragma mark - imageToTransparent
    152 
    153 void ProviderReleaseData (void *info, const void *data, size_t size){
    154 
    155     free((void*)data);
    156 
    157 }
    158 
    159 - (UIImage*)imageBlackToTransparent:(UIImage*)image withRed:(CGFloat)red andGreen:(CGFloat)green andBlue:(CGFloat)blue{
    160 
    161     const int imageWidth = image.size.width;
    162 
    163     const int imageHeight = image.size.height;
    164 
    165     size_t      bytesPerRow = imageWidth * 4;
    166 
    167     uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);
    168 
    169     // create context
    170 
    171     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    172 
    173     CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace,
    174 
    175                                                  kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
    176 
    177     CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage);
    178 
    179     // traverse pixe
    180 
    181     int pixelNum = imageWidth * imageHeight;
    182 
    183     uint32_t* pCurPtr = rgbImageBuf;
    184 
    185     for (int i = 0; i < pixelNum; i++, pCurPtr++){
    186 
    187         if ((*pCurPtr & 0xFFFFFF00) < 0x99999900){
    188 
    189             // change color
    190 
    191             uint8_t* ptr = (uint8_t*)pCurPtr;
    192 
    193             ptr[3] = red; //0~255
    194 
    195             ptr[2] = green;
    196 
    197             ptr[1] = blue;
    198 
    199         }else{
    200 
    201             uint8_t* ptr = (uint8_t*)pCurPtr;
    202 
    203             ptr[0] = 0;
    204 
    205         }
    206 
    207     }
    208 
    209     // context to image
    210 
    211     CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, ProviderReleaseData);
    212 
    213     CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight, 8, 32, bytesPerRow, colorSpace,
    214 
    215                                         kCGImageAlphaLast | kCGBitmapByteOrder32Little, dataProvider,
    216 
    217                                         NULL, true, kCGRenderingIntentDefault);
    218 
    219     CGDataProviderRelease(dataProvider);
    220 
    221     UIImage* resultUIImage = [UIImage imageWithCGImage:imageRef];
    222 
    223     // release
    224 
    225     CGImageRelease(imageRef);
    226 
    227     CGContextRelease(context);
    228 
    229     CGColorSpaceRelease(colorSpace);
    230 
    231     return resultUIImage;
    232 
    233 }
    234 
    235 @end
    236 
    237  
  • 相关阅读:
    使用mt_rand代替rand
    array_diff 不注意的坑
    stackoverflow 技术问答社区
    js检查浏览器是否处于隐身模式
    api数据接口
    图像识别api
    ionic creator(ionic生成器)
    商城金币设计
    details和summary标签
    iOS多线程编程之NSOperation的基本操作
  • 原文地址:https://www.cnblogs.com/lurenq/p/5338576.html
Copyright © 2020-2023  润新知