iOS development----Generate two-dimensional code

Source: Internet
Author: User

- (void) viewdidload {[Super viewdidload];     /*Note: 1. When generating two-dimensional code, it is not recommended to let the two-dimensional code to save too much data, because the more data, then the more dense two-dimensional code, then the more difficult to scan 2. Two-dimensional code has three anchor points, three anchor points can not be obscured, otherwise the scan does not come out 3. Two dimensional code Part can also scan the results normally, but it should be noted that this missing range is limited by the limit, if too much then also sweep the surface does not come out*/    //1. Create a filterCifilter *filter = [Cifilter filterwithname:@"Ciqrcodegenerator"]; //2. Restore filter Default Properties[Filter setdefaults]; //3. Convert data that needs to generate two-dimensional code to binaryNSData *data = [@"123"datausingencoding:nsutf8stringencoding]; //4. Set the data for the filter[Filter Setvalue:data Forkeypath:@"InputMessage"]; //5. Create a pictureCiimage *qrcodeimage =[Filter outputimage]; //6. Show Pictures//self.qrcodeImageView.image = [UIImage imagewithciimage:qrcodeimage];//self.qrcodeImageView.image = [self createnoninterpolateduiimageformciimage:qrcodeimage withsize:600];UIImage*BG = [Self createnoninterpolateduiimageformciimage:qrcodeimage withsize: -]; UIImage*icon = [UIImage imagenamed:@"icon"]; Self.qrcodeImageView.image=[self CRETEIMAGEWITHBG:BG Icon:icon];}-(UIImage *) CRETEIMAGEWITHBG: (UIImage *) bg icon: (UIImage *) icon{//1. Creating a Graphics contextUigraphicsbeginimagecontextwithoptions (Bg.size, YES,0.0); //2. Drawing the background[BG Drawinrect:cgrectmake (0,0, Bg.size.width, Bg.size.height)]; //3. Drawing IconsCGFloat iconw = -; CGFloat Iconh= -; CGFloat IconX= (BG.SIZE.WIDTH-ICONW) *0.5; CGFloat Icony= (BG.SIZE.HEIGHT-ICONH) *0.5;    [Icon Drawinrect:cgrectmake (IconX, Icony, Iconw, Iconh)]; //4. Remove the imageUIImage *newimage =Uigraphicsgetimagefromcurrentimagecontext (); //5. Close the contextUigraphicsendimagecontext (); //5. Return to Picture    returnnewimage;}/** * ciimage generate a specified size UIImage * * @param image Ciimage * @param size picture width*/-(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); return[UIImage imagewithcgimage:scaledimage];}

iOS development----Generate 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.