Refactor code for generate QRCode.Fixes issue #780.

This commit is contained in:
Qiu Yuzhou
2018-09-05 12:28:20 +08:00
parent 51c25cbc68
commit 3616b19652

View File

@ -23,9 +23,8 @@
} }
- (void)setQRCode:(NSString*) qrCode withOverlayText: (NSString*) text { - (void)setQRCode:(NSString*) qrCode withOverlayText: (NSString*) text {
CGImageRef cgImgRef = [self createQRImageForString:qrCode size:CGSizeMake(250, 250)]; NSImage *image = [self createQRImageForString:qrCode size:NSMakeSize(250, 250)];
NSImage *image = [[NSImage alloc]initWithCGImage:cgImgRef size:CGSizeMake(250, 250)];
if (text) { if (text) {
// Draw overlay text // Draw overlay text
NSDictionary* attrs = @{ NSDictionary* attrs = @{
@ -43,7 +42,10 @@
self.imageView.image = image; self.imageView.image = image;
} }
- (CGImageRef)createQRImageForString:(NSString *)string size:(CGSize)size { - (NSImage*)createQRImageForString:(NSString *)string size:(NSSize)size {
NSImage *outputImage = [[NSImage alloc]initWithSize:size];
[outputImage lockFocus];
// Setup the QR filter with our string // Setup the QR filter with our string
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[filter setDefaults]; [filter setDefaults];
@ -64,33 +66,19 @@
CGRect extent = CGRectIntegral(image.extent); CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size.width / CGRectGetWidth(extent), size.height / CGRectGetHeight(extent)); CGFloat scale = MIN(size.width / CGRectGetWidth(extent), size.height / CGRectGetHeight(extent));
// Since CoreImage nicely interpolates, we need to create a bitmap image that we'll draw into CGImageRef bitmapImage = [NSGraphicsContext.currentContext.CIContext createCGImage:image fromRect:extent];
// a bitmap context at the desired size;
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);
#if TARGET_OS_IPHONE CGContextRef graphicsContext = NSGraphicsContext.currentContext.CGContext;
CIContext *context = [CIContext contextWithOptions: @{kCIContextUseSoftwareRenderer: true}];
#else
CIContext *context = [CIContext contextWithCGContext:bitmapRef options:@{kCIContextUseSoftwareRenderer: @true}];
#endif
CGImageRef bitmapImage = [context createCGImage:image fromRect:extent]; CGContextSetInterpolationQuality(graphicsContext, kCGInterpolationNone);
CGContextScaleCTM(graphicsContext, scale, scale);
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone); CGContextDrawImage(graphicsContext, extent, bitmapImage);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
// Create an image with the contents of our bitmap
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
// Cleanup // Cleanup
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage); CGImageRelease(bitmapImage);
return scaledImage; [outputImage unlockFocus];
return outputImage;
} }
- (IBAction) copyQRCode: (id) sender{ - (IBAction) copyQRCode: (id) sender{