iOS native Cifilter Create two-dimensional code

Source: Internet
Author: User

iOS native Cifilter Create two-dimensional code2016-05-31 Future C

iOS native Cifilter Create two-dimensional code

About the two-dimensional code generation, online is also a lot, a long-ago third-party libraries are mostly written by C + +, but also some such as zxing, is also very useful, here is introduced through Cifilter to create two-dimensional code.

Creating the QR code is simple, just passing in a simple string .
-(Ciimage *) createqrforstring: (NSString *) qrstring {nsdata*stringdata =[qrstring datausingencoding:nsutf8stringencoding]; //Create filterCifilter*qrfilter = [Cifilter filterwithname:@"Ciqrcodegenerator"]; //setting content and error correction levels[Qrfilter setvalue:stringdata forkey:@"InputMessage"]; [Qrfilter SetValue:@"M"Forkey:@"Inputcorrectionlevel"]; //back to Ciimage    returnqrfilter.outputimage;}

For the generated QR code is a ciimage, we switch directly to uiimage word size is not good control, so use the following method to return the size of the UIImage:

-(UIImage *) Createnoninterpolateduiimageformciimage: (Ciimage *) Image withsize: (cgfloat) Size {cgrect extent=cgrectintegral (image.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:image fromrect:extent];    Cgcontextsetinterpolationquality (Bitmapref, Kcginterpolationnone);    CGCONTEXTSCALECTM (bitmapref, scale, scale);     Cgcontextdrawimage (Bitmapref, extent, bitmapImage); //2. Save Bitmap to Picturecgimageref scaledimage=cgbitmapcontextcreateimage (BITMAPREF);     Cgcontextrelease (BITMAPREF);     Cgimagerelease (BitmapImage); //OriginalUIImage*outputimage =[UIImage Imagewithcgimage:scaledimage];    Uigraphicsbeginimagecontextwithoptions (Outputimage.size, NO, [[UIScreen mainscreen] scale]); [Outputimage Drawinrect:cgrectmake (0,0, size, size)]; //Watermark MapUIImage*waterimage = [UIImage imagenamed:@"icon"]; [Waterimage drawinrect:cgrectmake (Size-waterimagesize)/2.0, (size-waterimagesize)/2.0, Waterimagesize, waterimagesize)]; UIImage*newpic =Uigraphicsgetimagefromcurrentimagecontext ();    Uigraphicsendimagecontext (); returnNewpic;}

Future CTO
Follow my CTO's path from now on.

Number: Wlaicto

iOS native Cifilter Create two-dimensional code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.