zoukankan      html  css  js  c++  java
  • iOS 7 二维码的生成

        //二维码生成
        
        //UIImageView *theImageView = [[UIImageView alloc]init];
        //[self.view addSubview:theImageView];
        
        CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
        [filter setDefaults];
        NSData *data = [@"hello worold!" dataUsingEncoding:NSUTF8StringEncoding];
        [filter setValue:data forKey:@"inputMessage"];
        
        CIImage *outputImage = [filter outputImage];
        
        CIContext *context = [CIContext contextWithOptions:nil];
        CGImageRef cgImage = [context createCGImage:outputImage
                                           fromRect:[outputImage extent]];
        
        UIImage *image = [UIImage imageWithCGImage:cgImage
                                             scale:1.
                                       orientation:UIImageOrientationUp];
        
        NSLog(@"image QRCode : %@",NSStringFromCGSize(image.size));
        // Resize without interpolating
        UIImage *resized = [self resizeImage:image
                                 withQuality:kCGInterpolationNone
                                        rate:5.0];
        
        NSLog(@"%@",NSStringFromCGSize(resized.size));
    
        UIImageView *theImageView =[[UIImageView alloc]initWithImage:resized];
        theImageView.frame = CGRectMake(100, 200, 100, 100);
        [self.view addSubview:theImageView];
        
        CGImageRelease(cgImage);
    

      由于生成的二维码和图片非常小,所以使用方法放大了5倍

    //Resize image
    
    - (UIImage *)resizeImage:(UIImage *)image
                 withQuality:(CGInterpolationQuality)quality
                        rate:(CGFloat)rate
    {
    	UIImage *resized = nil;
    	CGFloat width = image.size.width * rate;
    	CGFloat height = image.size.height * rate;
    	
    	UIGraphicsBeginImageContext(CGSizeMake(width, height));
    	CGContextRef context = UIGraphicsGetCurrentContext();
    	CGContextSetInterpolationQuality(context, quality);
    	[image drawInRect:CGRectMake(0, 0, width, height)];
    	resized = UIGraphicsGetImageFromCurrentImageContext();
    	UIGraphicsEndImageContext();
    	
    	return resized;
    }
    

      生成的结果:

              

  • 相关阅读:
    关于匹配的一些问题
    Codeforces Round #396 (Div. 2) A,B,C,D,E
    Codeforces Round #394 (Div. 2) A,B,C,D,E
    HDU 1848 SG函数博弈
    HDU 1536 S-Nim SG博弈
    HDU 2509 Be the Winner nim博弈变形
    HDU 1907 John nim博弈变形
    Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并
    BZOJ 1031: [JSOI2007]字符加密Cipher 后缀数组
    HDU 5769 Substring 后缀数组
  • 原文地址:https://www.cnblogs.com/cocoajin/p/3382501.html
Copyright © 2011-2022 走看看