kCAFilterNearest maginifcation filter(UIImageView) [英] kCAFilterNearest maginifcation filter (UIImageView)

查看:256
本文介绍了kCAFilterNearest maginifcation filter(UIImageView)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的QREncoder库位于这里: https://github.com/jverkoey/ObjQREncoder

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

基本上,我看着这个作者的示例代码,当他创建QRCode时,它完全没有像素化。该库提供的图像本身是33 x 33像素,但他使用kCAFilterNearest放大,使它非常清楚(没有像素化)。这是他的代码:

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];

我在xib中有一个UIImageView,我设置它的图像如下:

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];

但是qrcode真的很模糊。在例子中,它出来了透明。
我做错了什么?
谢谢!

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

更新:我发现问题不在于缩放或与kCAFFilterNearest相关。 这里是它的外观和它如何看起来像我保存UNGiew到PNG表示(注意QRCodes质量):

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):

>

UPDATE 2:这是我从UIView生成PNG文件:

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];


推荐答案

p>

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.

这篇关于kCAFilterNearest maginifcation filter(UIImageView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆