美文网首页
iOS生成二维码和条形码

iOS生成二维码和条形码

作者: zhiyuehl | 来源:发表于2017-11-15 14:53 被阅读0次

    代码基本很固定,做个笔记,留着以后需要。

    • 生成条形码
    /**
     *  生成条形码
     *
     *  @return 生成条形码的UIImage对象
     */
    + (UIImage *)resizeCodeWithString:(NSString *)text BCSize:(CGSize)size
    {
        CIImage *image = [self generateBarCodeImage:text];
        
        return [self resizeCodeImage:image withSize:size];
    }
    
    + (CIImage *)generateBarCodeImage:(NSString *)source
    {
        // iOS 8.0以上的系统才支持条形码的生成,iOS8.0以下使用第三方控件生成
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
            // 注意生成条形码的编码方式
            NSData *data = [source dataUsingEncoding: NSASCIIStringEncoding];
            CIFilter *filter = [CIFilter filterWithName:@"CICode128BarcodeGenerator"];
            [filter setValue:data forKey:@"inputMessage"];
            // 设置生成的条形码的上,下,左,右的margins的值
            [filter setValue:[NSNumber numberWithInteger:0] forKey:@"inputQuietSpace"];
            return filter.outputImage;
        }else{
            return nil;
        }
    }
    + (UIImage *)resizeCodeImage:(CIImage *)image withSize:(CGSize)size
    {
        if (image) {
            CGRect extent = CGRectIntegral(image.extent);
            CGFloat scaleWidth = size.width/CGRectGetWidth(extent);
            CGFloat scaleHeight = size.height/CGRectGetHeight(extent);
            size_t width = CGRectGetWidth(extent) * scaleWidth;
            size_t height = CGRectGetHeight(extent) * scaleHeight;
            CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();
            CGContextRef contentRef = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpaceRef, (CGBitmapInfo)kCGImageAlphaNone);
            CIContext *context = [CIContext contextWithOptions:nil];
            CGImageRef imageRef = [context createCGImage:image fromRect:extent];
            CGContextSetInterpolationQuality(contentRef, kCGInterpolationNone);
            CGContextScaleCTM(contentRef, scaleWidth, scaleHeight);
            CGContextDrawImage(contentRef, extent, imageRef);
            CGImageRef imageRefResized = CGBitmapContextCreateImage(contentRef);
            UIImage *barImage = [UIImage imageWithCGImage:imageRefResized];
    
            //Core Foundation 框架下内存泄露问题。
            CGContextRelease(contentRef);
            CGColorSpaceRelease(colorSpaceRef);
            CGImageRelease(imageRef);
            CGImageRelease(imageRefResized);
            return barImage;
        }else{
            return nil;
        }
    }
    
    • 生成二维码
    + (UIImage*)createQRWithString:(NSString*)text QRSize:(CGSize)size;
    {
        
        NSData *stringData = [text dataUsingEncoding: NSUTF8StringEncoding];
        //生成
        CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
        [qrFilter setValue:stringData forKey:@"inputMessage"];
        [qrFilter setValue:@"H" forKey:@"inputCorrectionLevel"];
        
         //上色
        CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor"
                                           keysAndValues:
                                 @"inputImage",qrFilter.outputImage,
                                 @"inputColor0",[CIColor colorWithCGColor:[UIColor blackColor].CGColor],
                                 @"inputColor1",[CIColor colorWithCGColor:[UIColor whiteColor].CGColor],
                                 nil];
        
        CIImage *qrImage = colorFilter.outputImage;
        
        //绘制
        CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:qrImage fromRect:qrImage.extent];
        UIGraphicsBeginImageContext(size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetInterpolationQuality(context, kCGInterpolationNone);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
        UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        CGImageRelease(cgImage);
        
        return codeImage;
    }
    

    最后

    附上扫描的三方库: LBXScan

    相关文章

      网友评论

          本文标题:iOS生成二维码和条形码

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