QrImage在iPhone 5、6、7中显示,但不在iPhone8和iPhone 10中显示。

时间:2018-10-25 09:10:17

标签: ios objective-c qr-code

CardVC *cardVC = [storyboard instantiateViewControllerWithIdentifier:@"cardVC"];
    cardVC.selectedCard = tempCard;
    [cardVC.view setTag:100];
    [self.view.window addSubview:cardVC.view];
    [self addChildViewController:cardVC];
    [self.view bringSubviewToFront:cardVC.view];
    [cardVC didMoveToParentViewController:self];

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.lblCardNo setText:[NSString stringWithFormat:@"%@ : %@",NSLocalizedString(@"lbl_Card_Number", nil),self.selectedCard.card_num]];
    [self.lblCardName setText:[NSString stringWithFormat:@"%@ : %@",NSLocalizedString(@"lbl_Card_Name", nil),self.selectedCard.card_name]];
    [self.lblExpiry setText:[NSString stringWithFormat:@"%@ : %@",NSLocalizedString(@"lbl_Card_Expiry", nil),[self getFormattedDateFromString:self.selectedCard.expirydate]]];
    [self.imgQR setImage:[UIImage mdQRCodeForString:self.selectedCard.card_num size:self.imgQR.bounds.size.width fillColor:[UIColor blackColor]]];
    [[self.cardView layer] setCornerRadius:10.0f];
}`
  

[未知进程名称] CGBitmapContextCreateImage:无效的上下文   0x0。

问题是我可以在iphone 6、6s,7中查看qrimage,但在iphone8和最新版本中却没有显示。所有设备都是ios12,因此应该不是ios版本问题。谁能帮我解决这个问题=((

+ (void)mdDrawQRCode:(QRcode *)code context:(CGContextRef)ctx size:(CGFloat)size fillColor:(UIColor *)fillColor {
    int margin = 0;
    unsigned char *data = code->data;
    int width = code->width;
    int totalWidth = width + margin * 2;
    int imageSize = (int)floorf(size);

    // @todo - review float->int stuff
    int pixelSize = imageSize / totalWidth;
    if (imageSize % totalWidth) {
        pixelSize = imageSize / width;
        margin = (imageSize - width * pixelSize) / 2;
    }

    CGRect rectDraw = CGRectMake(0.0f, 0.0f, pixelSize, pixelSize);
    // draw
    CGContextSetFillColorWithColor(ctx, fillColor.CGColor);
    for(int i = 0; i < width; ++i) {
        for(int j = 0; j < width; ++j) {
            if(*data & 1) {
                rectDraw.origin = CGPointMake(margin + j * pixelSize, margin + i * pixelSize);
                CGContextAddRect(ctx, rectDraw);
            }
            ++data;
        }
    }
    CGContextFillPath(ctx);
}

#pragma mark - public

+ (UIImage *)mdQRCodeForString:(NSString *)qrString size:(CGFloat)size {
    return [self mdQRCodeForString:qrString size:size fillColor:[UIColor blackColor]];
}

+ (UIImage *)mdQRCodeForString:(NSString *)qrString size:(CGFloat)imageSize fillColor:(UIColor *)fillColor {
    if (0 == [qrString length]) {
        return nil;
    }

    // generate QR
    QRcode *code = QRcode_encodeString([qrString UTF8String], 0, QR_ECLEVEL_L, QR_MODE_8, 1);
    if (!code) {
        return nil;
    }

    CGFloat size = imageSize * [[UIScreen mainScreen] scale];
    if (code->width > size) {
        printf("Image size is less than qr code size (%d)\n", code->width);
        return nil;
    }

    // create context
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    // The constants for specifying the alpha channel information are declared with the CGImageAlphaInfo type but can be passed to this parameter safely.

    CGContextRef ctx = CGBitmapContextCreate(0, size, size, 8, size * 4, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);

    CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(0, -size);
    CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1, -1);
    CGContextConcatCTM(ctx, CGAffineTransformConcat(translateTransform, scaleTransform));

    // draw QR on this context
    [self mdDrawQRCode:code context:ctx size:size fillColor:fillColor];

    // get image
    CGImageRef qrCGImage = CGBitmapContextCreateImage(ctx);
    UIImage * qrImage = [UIImage imageWithCGImage:qrCGImage];

    // free memory
    CGContextRelease(ctx);
    CGImageRelease(qrCGImage);
    CGColorSpaceRelease(colorSpace);
    QRcode_free(code);
    return qrImage;
}

@end

上面添加了库代码。

0 个答案:

没有答案