ios 生成二维码

    // 1.创建过滤器

    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];

    // 2.恢复默认

    [filtersetDefaults];

    // 3.给过滤器添加数据

    NSString *dataString = @"http://www.baidu.com";

    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];

    [filtersetValue:dataforKeyPath:@"inputMessage"];

    // 4.获取输出的二维码

    CIImage*outputImage = [filteroutputImage];

    // 5.将CIImage转换成UIImage,并放大显示

    self.imageView.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:200];

*************************************************************

上面代码生成的二维码图片比较虚,下面这个方法重绘二维码,让二维码更清晰

*************************************************************

/**

 *  根据CIImage生成指定大小的UIImage

 *

 *  @param image CIImage

 *  @param size 图片宽度

 */

- (UIImage*)createNonInterpolatedUIImageFormCIImage:(CIImage*)image withSize:(CGFloat) size

{

    CGRect extent = CGRectIntegral(image.extent);

    CGFloatscale =MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));

    // 1.创建bitmap;

    size_twidth =CGRectGetWidth(extent) * scale;

    size_theight =CGRectGetHeight(extent) * scale;

    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();

    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);

    CIContext *context = [CIContext contextWithOptions:nil];

    CGImageRefbitmapImage = [contextcreateCGImage:imagefromRect:extent];

    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);

    CGContextScaleCTM(bitmapRef, scale, scale);

    CGContextDrawImage(bitmapRef, extent, bitmapImage);


    // 2.保存bitmap到图片

    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);

    CGContextRelease(bitmapRef);

    CGImageRelease(bitmapImage);

    return [UIImage imageWithCGImage:scaledImage];

}


原文链接:http://www.cnblogs.com/davidyff/p/5254578.html

你可能感兴趣的:(ios 生成二维码)