Generation of two-dimensional code based on 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 development QR code generation