iOS 7 Core Image QR Code generation too blur

前端 未结 8 1576
心在旅途
心在旅途 2020-12-24 02:44

here\'s my code for generating QRCode image

+ (UIImage *)generateQRCodeWithString:(NSString *)string {
    NSData *stringData = [string dataUsingEncoding:NSU         


        
8条回答
  •  抹茶落季
    2020-12-24 03:14

    This method will use CoreImage to generate the QR code as a CIImage. Unfortunately, there's no simple way to disable interpolation, so scaling the image will create a blurry code. The workaround is to create temporary CGImageRef with the bits and draw it into a grayscale bitmap CGContextRef.

    Tested on OSX but should work on iOS as written.

    - (CGImageRef)createQRImageForString:(NSString *)string size:(CGSize)size {
      // Setup the QR filter with our string
      CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
      [filter setDefaults];
    
      NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
      [filter setValue:data forKey:@"inputMessage"];
      CIImage *image = [filter valueForKey:@"outputImage"];
    
      // Calculate the size of the generated image and the scale for the desired image size
      CGRect extent = CGRectIntegral(image.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
      // 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
      CIContext *context = [CIContext contextWithOptions:nil];
    #else
      CIContext *context = [CIContext contextWithCGContext:bitmapRef options:nil];
    #endif
    
      CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    
      CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
      CGContextScaleCTM(bitmapRef, scale, scale);
      CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
      // Create an image with the contents of our bitmap
      CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    
      // Cleanup
      CGContextRelease(bitmapRef);
      CGImageRelease(bitmapImage);
    
      return scaledImage;
    }
    

提交回复
热议问题