码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发 二维码生成

时间:2017-01-03 21:53:42      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:extc   ios开发   cal   details   unsigned   开发   oat   pre   add   

基于libqrencode的二维码生成

+ (void)drawQRCode:(QRcode *)code context:(CGContextRef)ctx size:(CGFloat)size {

unsigned char *data = 0;

int width;

data = code->data;

width = code->width;

float zoom = (double)size / (code->width + 2.0 * qr_margin);

CGRect rectDraw = CGRectMake(0, 0, zoom, zoom);

 

// draw

CGContextSetFillColor(ctx, CGColorGetComponents([UIColor blackColor].CGColor));

for(int i = 0; i < width; ++i) {

for(int j = 0; j < width; ++j) {

if(*data & 1) {

rectDraw.origin = CGPointMake((j + qr_margin) * zoom,(i + qr_margin) * zoom);

CGContextAddRect(ctx, rectDraw);

}

++data;

}

}

CGContextFillPath(ctx);

}

 

+ (UIImage *)qrImageForString:(NSString *)string imageSize:(CGFloat)size {

if (![string length]) {

return nil;

}

 

QRcode *code = QRcode_encodeString([string UTF8String], 0, QR_ECLEVEL_L, QR_MODE_8, 1);

if (!code) {

return nil;

}

 

// create context

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef ctx = CGBitmapContextCreate(0, size, size, 8, size * 4, colorSpace, kCGImageAlphaPremultipliedLast);

 

CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(0, -size);

CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1, -1);

CGContextConcatCTM(ctx, CGAffineTransformConcat(translateTransform, scaleTransform));

 

// draw QR on this context

[QRCodeGenerator drawQRCode:code context:ctx size:size];

 

// get image

CGImageRef qrCGImage = CGBitmapContextCreateImage(ctx);

UIImage * qrImage = [UIImage imageWithCGImage:qrCGImage];

 

// some releases

CGContextRelease(ctx);

CGImageRelease(qrCGImage);

CGColorSpaceRelease(colorSpace);

QRcode_free(code);

 

return qrImage;

}

 

iOS开发 二维码生成

标签:extc   ios开发   cal   details   unsigned   开发   oat   pre   add   

原文地址:http://www.cnblogs.com/diweinan/p/6246405.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!