kCAFilterNearest 放大过滤器 (UIImageView)
Posted
技术标签:
【中文标题】kCAFilterNearest 放大过滤器 (UIImageView)【英文标题】:kCAFilterNearest maginifcation filter (UIImageView) 【发布时间】:2011-06-30 21:32:49 【问题描述】:我正在使用此处找到的 QREncoder 库:https://github.com/jverkoey/ObjQREncoder
基本上,我查看了这位作者的示例代码,当他创建 QRCode 时,它完美地呈现出来,没有像素化。该库提供的图像本身是 33 x 33 像素,但他使用 kCAFilterNearest 放大并使其非常清晰(无像素化)。这是他的代码:
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,我正在设置它的图像:
[[template imageVQRCode] setImage:[QREncoder encode:ticketNum]];
[[[template imageVQRCode] layer] setMagnificationFilter:kCAFilterNearest];
但是二维码真的很模糊。在示例中,它非常清晰。 我究竟做错了什么? 谢谢!
更新:我发现问题不在于缩放或与 kCAFFilterNearest 有任何关系。它与从视图生成 PNG 图像有关。 这是它在 deive 上的外观与我将 UIView 保存为 PNG 表示时的外观(注意 QRCodes 质量):
更新 2:这就是我从 UIView 生成 PNG 文件的方式:
UIGraphicsBeginImageContextWithOptions([[template view] bounds].size, YES, 0.0);
[[[template view] layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[UIImagePNGRepresentation(viewImage) writeToFile:plistPath atomically:YES];
【问题讨论】:
我认为这需要一点澄清。您是否关心 QRCode 在视图中的显示或您从该视图创建的 PNG 文件? 感谢您的回复。问题在于生成的 PNG 文件。基本上,如您所见,模拟器视图中的 QRCode 比生成的 PNG 更清晰。我已使用用于生成 PNG 图像的代码更新了我的原始问题。 【参考方案1】:我用下面的函数来编辑图像。
- (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);
请找到下面的图片,如果您需要任何帮助,请告诉我。
【讨论】:
在if((bitmapInfo == kCGImageAlphaLast) || (bitmapInfo == kCGImageAlphaNone)) bitmapInfo = kCGImageAlphaNoneSkipLast;
行,为什么将CGBitmapInfo
变量与CGImageAlphaInfo
值进行比较?它给了我警告。以上是关于kCAFilterNearest 放大过滤器 (UIImageView)的主要内容,如果未能解决你的问题,请参考以下文章