//generate QR code -(void)onclick_qrcode:(id)sender{ // 1. Create a QR code filter instance (CIFilter) CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; // filter to restore default settings [filter setDefaults]; // 2. Add data to the filter NSString *string = @"Here is the text that needs to generate a QR code"; NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; // Assign value to filter using KVC [filter setValue:data forKeyPath:@"inputMessage"]; // 3. Generate QR code CIImage *image = [filter outputImage]; // image = [image imageByApplyingTransform:CGAffineTransformMakeScale(200, 200)]; // Convert to the type of UI // UIImage *qrUIImage = [UIImage imageWithCIImage:image]; UIImage *qrUIImage = [self createNonInterpolatedUIImageFormCIImage:image withSize:200]; //white box UIImage* whiteImg = [ColorUtil colorToImage:[ColorUtil colorWithHexString:COLOR_WHITE]]; //----------------Add a custom image to the middle of the QR code---------------- //Open the drawing and get the graphics context (the size of the context is the size of the QR code) UIGraphicsBeginImageContext(qrUIImage.size); //Draw the QR code picture. (Here is, the graphics context, the upper left corner is the (0,0) point) [qrUIImage drawInRect:CGRectMake(0, 0, qrUIImage.size.width, qrUIImage.size.height)]; // draw the small picture again UIImage *sImage = [UIImage imageNamed:@"app_logo"]; CGFloat sImageW = qrUIImage.size.width/4; CGFloat sImageH= sImageW; CGFloat sImageX = (qrUIImage.size.width - sImageW) * 0.5; CGFloat sImgaeY = (qrUIImage.size.height - sImageH) * 0.5; // draw rounded background [[UIBezierPath bezierPathWithRoundedRect:CGRectMake(sImageX, sImgaeY, sImageW, sImageH) cornerRadius:5] addClip]; [whiteImg drawInRect:CGRectMake(sImageX, sImgaeY, sImageW, sImageH)]; // draw rounded logo [[UIBezierPath bezierPathWithRoundedRect:CGRectMake(sImageX+3, sImgaeY+3, sImageW-6, sImageH-6) cornerRadius:5] addClip]; [sImage drawInRect:CGRectMake(sImageX+3, sImgaeY+3, sImageW-6, sImageH-6)]; //Get this picture that is currently drawn UIImage *finalyImage = UIGraphicsGetImageFromCurrentImageContext(); //Close the graphics context UIGraphicsEndImageContext(); // 4. Display QR code MyLinearLayout* qrcodeLayout = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert]; qrcodeLayout.myMargin = 0; qrcodeLayout.backgroundColor = [ColorUtil colorWithHexString:COLOR_BLACK alpha:.6]; qrcodeLayout.gravity = MyMarginGravity_Center; [qrcodeLayout setTarget:self action:@selector(onclick_qrcode_close:)]; [self.view addSubview:qrcodeLayout]; UIImageView* qrcodeImg = [UIImageView new]; qrcodeImg.widthDime.equalTo(@200); qrcodeImg.heightDime.equalTo(@200); // qrcodeImg.image = [self createNonInterpolatedUIImageFormCIImage:image withSize:200]; qrcodeImg.image = finalyImage; [qrcodeLayout addSubview:qrcodeImg]; } - (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat)size { CGRect extent = CGRectIntegral(image.extent); //set scale CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent)); // create bitmap(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); // save bitmap to image CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef); CGContextRelease(bitmapRef); CGImageRelease(bitmapImage); return [UIImage imageWithCGImage:scaledImage]; } -(void)onclick_qrcode_close:(MyLinearLayout*)layout{ [layout removeFromSuperview]; }
ios generates a QR code with a rounded border and a rounded logo image in the middle
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326171241&siteId=291194637
Ranking