I. Need to include header files
#import <CoreImage/CoreImage.h>
Two. Sample code--The following generated QR code is not clear enough:
1- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{2 3 //0. Import header file #import <CoreImage/CoreImage.h>4 5 //1. Create a filter-Apple does not encapsulate this character as a constant6Cifilter *filter = [Cifilter filterwithname:@"Ciqrcodegenerator"];7 8 //2. Filter restore default Settings9 [Filter setdefaults];Ten One //3. Add data to the filter (regular expression/account number and password)--set filter by KVC, only set NSData type ANSString *datastring =@"http://www.baidu.com"; -NSData *data =[datastring datausingencoding:nsutf8stringencoding]; -[Filter Setvalue:data Forkeypath:@"InputMessage"]; the - //4. Get the output of the QR code -Ciimage *outputimage =[Filter outputimage]; - + //5. Display two-dimensional code -Self.imageView.image =[UIImage imagewithciimage:outputimage]; +}
Three. Call the following method can generate a clear QR code image, when used can also be added to the UIImage a classification. There are no categories to extract.:
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]; -}
iOS development-two-D code generation