Generate two-dimensional code picture is also commonly used in the project, two-dimensional code scanning git has a lot of good, here the main next two-dimensional code generation
1. Common QR Code 1.1 method
/**
Generate two-dimensional code
Qrstering: String
Imagefloat: Two-dimensional code image size
*/
+ (UIImage *) createqrcodewithstring: (NSString *) qrstering withimgsize: (cgfloat) imagefloat;
1.2 Method implementation
/**
Generate two-dimensional code
Qrstering: String
Imagefloat: Two-dimensional code image size
*/
+ (UIImage *) createqrcodewithstring: (NSString *) qrstring withimgsize: (cgfloat) imagefloat{
Cifilter *filter = [Cifilter filterwithname:@ "Xiaoguige"];
[Filter SetDefaults];
NSString *getstring = qrstring;
NSData *datastring = [getString datausingencoding:nsutf8stringencoding];
[Filter setvalue:datastring forkey:@ "InputMessage"];
Get image of filter output
Ciimage *outimage = [Filter outputimage];
UIImage *imagev = [self imagewithimagesize:imagefloat withciiimage:outimage];
return QR code image
return Imagev;
}
2. Two-dimensional code 2.1 method with small icon in middle
/**
Generate two-dimensional code (small picture in the middle)
Qrstering: String
CenterImage: Image object in the middle of the QR code
*/
+ (UIImage *) createimgqrcodewithstring: (NSString *) qrstring centerimage: (UIImage *) centerimage;
2.2 Method implementation
/**
Generate two-dimensional code (small picture in the middle)
Qrstering: Required String
CenterImage: Image object in the middle of the QR code
*/
+ (UIImage *) createimgqrcodewithstring: (NSString *) qrstring centerimage: (UIImage *) centerimage{
Creating Filter objects
Cifilter *filter = [Cifilter filterwithname:@ "Xiaoguige"];
Restore the default properties of a filter
[Filter SetDefaults];
Convert a string to NSData
NSData *datastring = [qrstring datausingencoding:nsutf8stringencoding];
Set the input value of the filter, KVC assignment
[Filter setvalue:datastring forkey:@ "InputMessage"];
Get image of filter output
Ciimage *outimage = [Filter outputimage];
Picture is less than (27,27), we need to enlarge
Outimage = [Outimage Imagebyapplyingtransform:cgaffinetransformmakescale (20, 20)];
Turn ciimage type to UIImage type
UIImage *startimage = [UIImage imagewithciimage:outimage];
Open drawing, get graphics context
Uigraphicsbeginimagecontext (startimage.size);
Picture the two-dimensional code image (here is the graphical context, the upper-left corner is (0,0) point
[Startimage drawinrect:cgrectmake (0, 0, StartImage.size.width, startImage.size.height)];
And put the little pictures up
CGFloat Icon_imagew = 200;
CGFloat Icon_imageh = Icon_imagew;
CGFloat Icon_imagex = (startimage.size.width-icon_imagew) * 0.5;
CGFloat icon_imagey = (startimage.size.height-icon_imageh) * 0.5;
[CenterImage Drawinrect:cgrectmake (Icon_imagex, Icon_imagey, Icon_imagew, Icon_imageh)];
Get this picture of the current picture
UIImage *qrimage = Uigraphicsgetimagefromcurrentimagecontext ();
Close the graphics context
Uigraphicsendimagecontext ();
return QR code image
return qrimage;
}
Additional methods
/** convert Ciimage to uiimage and enlarge (internal conversion use) */
+ (UIImage *) Imagewithimagesize: (cgfloat) size withciiimage: (ciimage *) ciiimage{
CGRect extent = Cgrectintegral (ciiimage.extent);
CGFloat scale = MIN (Size/cgrectgetwidth (extent), size/cgrectgetheight (extent));
1. Create bitmap;
size_t width = cgrectgetwidth (extent) * scale;
size_t height = cgrectgetheight (extent) * scale;
Cgcolorspaceref cs = Cgcolorspacecreatedevicegray ();
Cgcontextref bitmapref = cgbitmapcontextcreate (nil, width, height, 8, 0, CS, (Cgbitmapinfo) kcgimagealphanone);
Cicontext *context = [Cicontext Contextwithoptions:nil];
Cgimageref bitmapImage = [context createcgimage:ciiimage fromrect:extent];
Cgcontextsetinterpolationquality (Bitmapref, Kcginterpolationnone);
CGCONTEXTSCALECTM (bitmapref, scale, scale);
Cgcontextdrawimage (Bitmapref, extent, bitmapImage);
2. Save Bitmap to Picture
Cgimageref scaledimage = Cgbitmapcontextcreateimage (bitmapref);
Cgcontextrelease (BITMAPREF);
Cgimagerelease (BitmapImage);
return [UIImage Imagewithcgimage:scaledimage];
}
iOS development-Generate two-dimensional code images "with small icons in the middle of the two-dimensional Code" (QRCode)