2

I am using QREncoder library found here: https://github.com/jverkoey/ObjQREncoder

Basically, i looked at the example code by this author, and when he creates the QRCode it comes out perfectly with no pixelation. The image itself that the library provides is 33 x 33 pixels, but he uses kCAFilterNearest to magnify and make it very clear (no pixilation). Here is his code:

    UIImage* image = [QREncoder encode:@"http://www.google.com/"];

    UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
  CGFloat qrSize = self.view.bounds.size.width - kPadding * 2;
    imageView.frame = CGRectMake(kPadding, (self.view.bounds.size.height - qrSize) / 2,
    qrSize, qrSize);
    [imageView layer].magnificationFilter = kCAFilterNearest;

    [self.view addSubview:imageView];

I have a UIImageView in a xib, and I am setting it's image like this:

[[template imageVQRCode] setImage:[QREncoder encode:ticketNum]];
[[[template imageVQRCode] layer] setMagnificationFilter:kCAFilterNearest];

but the qrcode is really blurry. In the example, it comes out crystal clear. What am i doing wrong? Thanks!

UPDATE:I found out that the problem isn't with scaling or anything to do with kCAFFilterNearest. It has to do with generating the PNG image from the view. Here's how it looks on the deive vs how it looks like when i save the UIView to the PNG representation (Notice the QRCodes quality):

enter image description here

UPDATE 2: This is how I am generating the PNG file from UIView:

   UIGraphicsBeginImageContextWithOptions([[template view] bounds].size, YES, 0.0);
    [[[template view] layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [UIImagePNGRepresentation(viewImage) writeToFile:plistPath atomically:YES];
sumderungHAY
  • 1,337
  • 18
  • 30
  • I think this needs a little clarification. Are you concerned with the display of the QRCode in the view or with the PNG file you create from that view? – Corin Jun 30 '11 at 22:10
  • Thanks for replying. the issue is with the PNG file that is generated. Basically, as you can see, the QRCode in the view on simulator is way more sharp and clear than in PNG generated. I have updated my original question with the code that I am using to generate the PNG image. – sumderungHAY Jun 30 '11 at 22:12

1 Answers1

0

I have used below function for editing image.

- (UIImage *)resizedImage:(CGSize)newSize interpolationQuality:(CGInterpolationQuality)quality
{
    BOOL drawTransposed;

    switch (self.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            drawTransposed = YES;
            break;

        default:
            drawTransposed = NO;
    }

    return [self resizedImage:newSize
                    transform:[self transformForOrientation:newSize]
               drawTransposed:drawTransposed
         interpolationQuality:quality];

}

- (UIImage *)resizedImage:(CGSize)newSize
                transform:(CGAffineTransform)transform
           drawTransposed:(BOOL)transpose
     interpolationQuality:(CGInterpolationQuality)quality
{

    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
    CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width);
    CGImageRef imageRef = self.CGImage;

    // Build a context that's the same dimensions as the new size
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
    if((bitmapInfo == kCGImageAlphaLast) || (bitmapInfo == kCGImageAlphaNone))
        bitmapInfo = kCGImageAlphaNoneSkipLast;

    CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                                newRect.size.width,
                                                newRect.size.height,
                                                CGImageGetBitsPerComponent(imageRef),
                                                0,
                                                CGImageGetColorSpace(imageRef),
                                                bitmapInfo);

    // Rotate and/or flip the image if required by its orientation
    CGContextConcatCTM(bitmap, transform);

    // Set the quality level to use when rescaling
    CGContextSetInterpolationQuality(bitmap, quality);

    // Draw into the context; this scales the image
    CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef);

    // Get the resized image from the context and a UIImage
    CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

    // Clean up
    CGContextRelease(bitmap);
    CGImageRelease(newImageRef);

    return newImage;

}


UIImageWriteToSavedPhotosAlbum([image resizedImage:CGSizeMake(300, 300) interpolationQuality:kCGInterpolationNone], nil, nil, nil);

Please find below image and let me know if you need any help.

enter image description here

elp
  • 8,021
  • 7
  • 61
  • 120
iPhone developer.
  • 2,122
  • 1
  • 16
  • 17
  • At the line `if((bitmapInfo == kCGImageAlphaLast) || (bitmapInfo == kCGImageAlphaNone)) bitmapInfo = kCGImageAlphaNoneSkipLast;`, why do you compare `CGBitmapInfo` variable with `CGImageAlphaInfo` values? It gives me warnings. – Hlung Jan 14 '14 at 16:54