xcode10后,生成二维码失效 If you want to see the backtrace, please set CG_CONTEXT_SHOW_BA

617 阅读1分钟

需要导入:QuartzCore.framework,CoreVideo.framework,CoreMedia.framework,AVFoundation.framework,CoreImage.framework

  调用:  [self.QRImgview setImage:[Common qrCodeImageWithString:[Common dictionaryToJson:self.dic]]];


+(UIImage*)qrCodeImageWithString:(NSString*)str{

if (str && [str isKindOfClass:[NSString class]] && str.length > 0) {

return [self createNonInterpolatedUIImageFromCIImage:[self createQRForString:str]

withScale:2*[[UIScreen mainScreen] scale]];

}

return nil;

}

+ (CIImage *)createQRForString:(NSString *)qrString

{

// Need to convert the string to a UTF-8 encoded NSData object

NSData *stringData = [qrString dataUsingEncoding: NSUTF8StringEncoding];

// Create the filter

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

// Set the message content and error-correction level

[qrFilter setValue:stringData forKey:@"inputMessage"];

[qrFilter setValue:@"H" forKey:@"inputCorrectionLevel"];

// Send the image back

return qrFilter.outputImage;

}


+ (UIImage *)createNonInterpolatedUIImageFromCIImage:(CIImage *)image withScale:(CGFloat)scale

{

// Render the CIImage into a CGImage

CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:image fromRect:image.extent];

// Now we'll rescale using CoreGraphics

UIGraphicsBeginImageContext(CGSizeMake(image.extent.size.width * scale, image.extent.size.width * scale));

CGContextRef context = UIGraphicsGetCurrentContext();

// We don't want to interpolate (since we've got a pixel-correct image)

CGContextSetInterpolationQuality(context, kCGInterpolationNone);

CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);

// Get the image out

UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();

// Tidy up

UIGraphicsEndImageContext();

CGImageRelease(cgImage);

return scaledImage;

}