二维码解析

2017-04-06  本文已影响8人  张家杰仔
- (void)initUserInterface{
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    /**  恢复默认 */
    [filter setDefaults];
    /**  给过滤器添加地址 */
    NSString *dataString = @"https://www.baidu.com";
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    /**  设置滤镜数据 */
    [filter setValue:data forKeyPath:@"inputMessage"];
    /**  获取输出的二维码 */
    CIImage *myQrImage = [filter outputImage];
    self.qrCodeView.image = [self createNonInterpolatedUIImageFormCIImage:myQrImage withSize:200];
    [self addSubview:self.qrCodeView];
}
/**  高清化二维码 */
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    /**  创建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);
    /**  保存bitmap到图片 */
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}


上一篇下一篇

猜你喜欢

热点阅读