美文网首页
二维码解析

二维码解析

作者: 张家杰仔 | 来源:发表于2017-04-06 10:41 被阅读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];
    }
    
    
    

    相关文章

      网友评论

          本文标题:二维码解析

          本文链接:https://www.haomeiwen.com/subject/orutattx.html