First, preface:
iOS7.0 can then use the system native API to generate a QR code, iOS8.0 can be generated after the barcode, the system default generated color is black. Here, the following methods can be used to generate a specified size, specify the color of the QR code and bar code, you can also add background color, shadow effect, the following is the specific method.
Two or two-dimensional code generation
Because the return is uiimage, so I wrote a uiimage classification.
UIimage (qrcode). h file//avilable in IOS 7.0 and later+ (UIimage *) Qrcodeimagewithcontent: (NSString *) content Codeimagesize: (cgfloat) Size logo: (UIImage *) logo logoframe :(CGRect) Logoframe red: (cgfloat) Red Green: (cgfloat) Green Blue: (Nsinteger) blue;//UIimage (QRCode). m implementation file #pragma mark-Generate two-dimensional code + (UIimage *) Qrcodeimagewithcon Tent: (NSString *) content codeimagesize: (cgfloat) Size logo: (UIImage *) lo Go logoframe: (cgrect) Logoframe red: (cgfloat) Red Green: (cgfloat) Green Blue: (nsinteger) blue{UIImage *image = [Self Qrcodeimagew Ithcontent:content codeimagesize:size red:red Green:green Blue:blue]; Logo on Logo if (logo! = nil) {UigraphicsbeginimagecontexT (image.size); [Image drawinrect:cgrectmake (0, 0, Image.size.width, image.size.height)]; [Logo drawinrect:logoframe]; UIImage *resultimage = Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); return resultimage; }else{return image; }}//change two-dimensional code color + (UIImage *) Qrcodeimagewithcontent: (NSString *) content codeimagesize: (cgfloat) Size red: (cgfloat) Red Green: (cgfloat) Green Blue: (nsinteger) blue{UIImage *image = [self qrcodeimagewithcontent:content codeimagesize:size]; int imagewidth = Image.size.width; int imageheight = Image.size.height; size_t Bytesperrow = imagewidth * 4; uint32_t *rgbimagebuf = (uint32_t *) malloc (Bytesperrow * imageheight); Cgcolorspaceref colorspaceref = Cgcolorspacecreatedevicergb (); Cgcontextref context = Cgbitmapcontextcreate (Rgbimagebuf, ImageWidth, ImageHeight, 8, Bytesperrow, ColorSpaceRef, Kcgbitmapbyteorder32little|kcgimagealphanoneskiplast); Cgcontextdrawimage (Context, CGRectmake (0, 0, ImageWidth, imageheight), image. Cgimage); traversing pixels, changing pixel color int pixelnum = ImageWidth * imageheight; uint32_t *pcurptr = rgbimagebuf; for (int i = 0; i<pixelnum; i++, pcurptr++) {if (*pcurptr & 0xffffff00) < 0x99999900) {UINT 8_t* ptr = (uint8_t*) pcurptr; PTR[3] = red*255; PTR[2] = green*255; PTR[1] = blue*255; }else{uint8_t* ptr = (uint8_t*) pcurptr; Ptr[0] = 0; }}//Take out picture cgdataproviderref Dataprovider = Cgdataprovidercreatewithdata (NULL, Rgbimagebuf, Bytesperrow * imageHe ight, Providerreleasedata); Cgimageref imageref = cgimagecreate (ImageWidth, ImageHeight, 8, +, Bytesperrow, Colorspaceref, Kcgimagealphalast | Kcgbitmapbyteorder32little, Dataprovider, NULL, True, Kcgrenderingintentdefault); Cgdataproviderrelease (Dataprovider); UIImage *resultimage = [UIImage ImagewITHCGIMAGE:IMAGEREF]; Cgimagerelease (IMAGEREF); Cgcontextrelease (context); Cgcolorspacerelease (COLORSPACEREF); return resultimage;} Change the dimension size of the QR code + (UIImage *) Qrcodeimagewithcontent: (NSString *) content codeimagesize: (cgfloat) size{ciimage *image = [self Qrcodeimagewithcontent:content]; CGRect integralrect = cgrectintegral (image.extent); CGFloat scale = MIN (Size/cgrectgetwidth (Integralrect), Size/cgrectgetheight (Integralrect)); size_t width = cgrectgetwidth (integralrect) *scale; size_t height = cgrectgetheight (integralrect) *scale; Cgcolorspaceref colorspaceref = Cgcolorspacecreatedevicegray (); Cgcontextref bitmapref = cgbitmapcontextcreate (nil, width, height, 8, 0, Colorspaceref, (Cgbitmapinfo) Kcgimagealphanone ); Cicontext *context = [Cicontext Contextwithoptions:nil]; Cgimageref bitmapImage = [context createcgimage:image Fromrect:integralrect]; Cgcontextsetinterpolationquality (Bitmapref, Kcginterpolationnone); CGCONTEXTSCALECTM (bitmapref, scale,scale); Cgcontextdrawimage (Bitmapref, Integralrect, bitmapImage); Cgimageref scaledimage = Cgbitmapcontextcreateimage (bitmapref); Cgcontextrelease (BITMAPREF); Cgimagerelease (BitmapImage); return [UIImage imagewithcgimage:scaledimage];} Generate the most original QR code + (Ciimage *) Qrcodeimagewithcontent: (NSString *) content{cifilter *qrfilter = [Cifilter filterwithname:@ "CI Qrcodegenerator "]; NSData *contentdata = [content datausingencoding:nsutf8stringencoding]; [Qrfilter setvalue:contentdata forkey:@ "InputMessage"]; [Qrfilter setvalue:@ "H" forkey:@ "Inputcorrectionlevel"]; Ciimage *image = qrfilter.outputimage; return image; void Providerreleasedata (void *info, const void *data, size_t size) {free ((void*) data);}
Barcode Generation
. h file #pragma mark-Generate barcodes//avilable in IOS 8.0 and later+ (UIImage *) Barcodeimagewithcontent: (NSString *) content Codeimagesize: (cgsize) Size red: (cgfloat) Red g Reen: (cgfloat) Green Blue: (Nsinteger) blue; #pragma mark-Generate barcode + (UIImage *) barcodeimagewithc Ontent: (NSString *) content codeimagesize: (cgsize) Size red: (cgfloat) Red Green: (cgfloat) Green Blue: (Nsinteger) blue{ UIImage *image = [self barcodeimagewithcontent:content codeimagesize:size]; int imagewidth = Image.size.width; int imageheight = Image.size.height; size_t Bytesperrow = imagewidth * 4; uint32_t *rgbimagebuf = (uint32_t *) malloc (Bytesperrow * imageheight); Cgcolorspaceref colorspaceref = Cgcolorspacecreatedevicergb (); Cgcontextref context = Cgbitmapcontextcreate (Rgbimagebuf, ImageWidth, ImageHeight, 8, Bytesperrow, ColorSpaceRef, Kcgbitmapbyteorder32little|kcgimagealphanoneskiplast); CgcontexTdrawimage (context, CGRectMake (0, 0, ImageWidth, imageheight), image. Cgimage); traversing pixels, changing pixel color int pixelnum = ImageWidth * imageheight; uint32_t *pcurptr = rgbimagebuf; for (int i = 0; i<pixelnum; i++, pcurptr++) {if (*pcurptr & 0xffffff00) < 0x99999900) {UINT 8_t* ptr = (uint8_t*) pcurptr; PTR[3] = red*255; PTR[2] = green*255; PTR[1] = blue*255; }else{uint8_t* ptr = (uint8_t*) pcurptr; Ptr[0] = 0; }}//Take out picture cgdataproviderref Dataprovider = Cgdataprovidercreatewithdata (NULL, Rgbimagebuf, Bytesperrow * imageHe ight, Providerreleasedata); Cgimageref imageref = cgimagecreate (ImageWidth, ImageHeight, 8, +, Bytesperrow, Colorspaceref, Kcgimagealphalast | Kcgbitmapbyteorder32little, Dataprovider, NULL, True, Kcgrenderingintentdefault); Cgdataproviderrelease (Dataprovider); UIImage *resultiMage = [UIImage imagewithcgimage:imageref]; Cgimagerelease (IMAGEREF); Cgcontextrelease (context); Cgcolorspacerelease (COLORSPACEREF); return resultimage;} Change barcode size + (UIImage *) Barcodeimagewithcontent: (NSString *) content codeimagesize: (cgsize) size{ciimage *image = [self Barcodeimagewithcontent:content]; CGRect integralrect = cgrectintegral (image.extent); CGFloat scale = MIN (Size.width/cgrectgetwidth (Integralrect), Size.height/cgrectgetheight (Integralrect)); size_t width = cgrectgetwidth (integralrect) *scale; size_t height = cgrectgetheight (integralrect) *scale; Cgcolorspaceref colorspaceref = Cgcolorspacecreatedevicegray (); Cgcontextref bitmapref = cgbitmapcontextcreate (nil, width, height, 8, 0, Colorspaceref, (Cgbitmapinfo) Kcgimagealphanone ); Cicontext *context = [Cicontext Contextwithoptions:nil]; Cgimageref bitmapImage = [context createcgimage:image Fromrect:integralrect]; Cgcontextsetinterpolationquality (Bitmapref, Kcginterpolationnone);CGCONTEXTSCALECTM (bitmapref, scale, scale); Cgcontextdrawimage (Bitmapref, Integralrect, bitmapImage); Cgimageref scaledimage = Cgbitmapcontextcreateimage (bitmapref); Cgcontextrelease (BITMAPREF); Cgimagerelease (BitmapImage); return [UIImage imagewithcgimage:scaledimage];} Generate the most original barcode + (Ciimage *) Barcodeimagewithcontent: (NSString *) content{cifilter *qrfilter = [Cifilter filterwithname:@ "C Icode128barcodegenerator "]; NSData *contentdata = [content datausingencoding:nsutf8stringencoding]; [Qrfilter setvalue:contentdata forkey:@ "InputMessage"]; [Qrfilter setvalue:@ (0.00) forkey:@ "Inputquietspace"]; Ciimage *image = qrfilter.outputimage; return image; void Providerreleasedata (void *info, const void *data, size_t size) {free ((void*) data);}
IV, testing
Implement Self.title = @ "QR Code and barcode" in the controller; Barcode UIImage *barimage = [UIImage barcodeimagewithcontent:@ "123456" Codeim Agesize:cgsizemake (+) red:0 green:0.4 blue:0.6]; CGRect barimageview_frame = CGRectMake (SELF.VIEW.BOUNDS.SIZE.WIDTH/2-300/2, 100, 300, 90); Uiimageview *barimageview = [[Uiimageview alloc] initwithframe:barimageview_frame]; Barimageview.image = Barimage; Barimageview.backgroundcolor = [Uicolor Clearcolor]; Shadow BarImageView.layer.shadowOffset = Cgsizemake (-0.5, 0.5); BarImageView.layer.shadowRadius = 0.5; BarImageView.layer.shadowColor = [Uicolor blackcolor]. Cgcolor; barImageView.layer.shadowOpacity = 0.2; [Self.view Addsubview:barimageview]; QR code UIImage *qrcodeimage = [UIImage qrcodeimagewithcontent:@ "15280107764" codeimagesize:200 logo:[uiimage Image named:@ "Avatar"] logoframe:cgrectmake (75, 75, 50, 50) red:0.0f green:139/255.0f BLUE:139/255.0F]; CGRect qrcodeimageview_frame = CGRectMake (SELF.VIEW.BOUNDS.SIZE.WIDTH/2-200/2, Cgrectgetmaxy (barImageView.frame) + 20, 200, 200); Uiimageview *qrcodeimageview = [[Uiimageview alloc] initwithframe:qrcodeimageview_frame]; Qrcodeimageview.image = Qrcodeimage; Qrcodeimageview.backgroundcolor = [Uicolor whitecolor];//#006400//Shadow QrCodeImageView.layer.shadowOffset = Cgsizema Ke (0, 0); QrCodeImageView.layer.shadowRadius = 5; QrCodeImageView.layer.shadowColor = [Uicolor blackcolor]. Cgcolor; QrCodeImageView.layer.shadowOpacity = 0.4; [Self. View Addsubview:qrcodeimageview];
Reference source http://www.jianshu.com/p/b893690b472e
iOS QR code, Barcode generation (can specify size, color)