One or two-dimensional code generation
- The generation and reading of two-dimensional code has been integrated from iOS7
- Previously widely used ZBARSDK does not currently support 64-bit processors, in addition to the ZXINGSDK can also generate two-dimensional code
- Steps to generate a QR code
- Import Coreimage Framework
- Generate two-dimensional code with filter Cifilter
- QR Code content (traditional barcode can only put numbers)
- Plain text
- Card
- URL (can jump to page directly)
The specific code generated by the QR code
1- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event {2 3 //Creating Filters4Cifilter *filter = [Cifilter filterwithname:@"Ciqrcodegenerator"];5 6 //Filter Restore Default7 [Filter setdefaults];8 9 //adding data to a filterTenNSString *string=@"http://www.cnblogs.com/PSSSCode/"; One A //Convert nsstring format to nsdata format -NSData *data = [stringdatausingencoding:nsutf8stringencoding Allowlossyconversion:yes]; - the[Filter Setvalue:data Forkeypath:@"InputMessage"]; - - //obtain QR code generated by two-dimensional code filter -Ciimage *image =[Filter outputimage]; + - //Add the obtained QR code to the ImageView +Self.imageView.image =[uiimage Imagewithciimage:image];
We can see that the generated QR code is blurred, and the last step of the block is replaced by the method called in the code block below.
1 /**2 * Generate UIImage of the specified size according to Ciimage3 *4 * @param image Ciimage5 * @param size picture width6 */7-(UIImage *) Createnoninterpolateduiimageformciimage: (Ciimage *) Image withsize: (cgfloat) Size8 {9CGRect extent =cgrectintegral (image.extent);TenCGFloat scale = MIN (Size/cgrectgetwidth (extent), size/Cgrectgetheight (extent)); One A //1. Create bitmap; -size_t width = cgrectgetwidth (extent) *Scale ; -size_t height = cgrectgetheight (extent) *Scale ; theCgcolorspaceref cs =Cgcolorspacecreatedevicegray (); -Cgcontextref bitmapref = cgbitmapcontextcreate (nil, width, height,8,0, CS, (Cgbitmapinfo) kcgimagealphanone); -Cicontext *context =[Cicontext Contextwithoptions:nil]; -Cgimageref BitmapImage =[Context Createcgimage:image fromrect:extent]; + cgcontextsetinterpolationquality (Bitmapref, kcginterpolationnone); - CGCONTEXTSCALECTM (bitmapref, scale, scale); + Cgcontextdrawimage (Bitmapref, extent, bitmapImage); A at //2. Save Bitmap to Picture -Cgimageref Scaledimage =cgbitmapcontextcreateimage (bitmapref); - cgcontextrelease (bitmapref); - cgimagerelease (bitmapImage); - return[UIImage imagewithcgimage:scaledimage]; -}
Two-dimensional code generated by normal code generated QR code
Native framework in iOS generates QR code