有什么方法可以在 Objective-C / iPhone / iOS SDK 中禁用 CALayer 的 rasterizationScale 插值/抗锯齿功能吗?

发布于 2024-11-30 07:37:46 字数 1435 浏览 1 评论 0原文

我想在设置 myLayer.rasterizationScale = 0.01myLayer.shouldRasterize = YES; 时摆脱任何插值/抗锯齿/等

示例: 在此处输入图像描述

这是我正在尝试的代码: <代码>

- (void)drawRect:(CGRect)rect {
  CGContextRef ctx = UIGraphicsGetCurrentContext();
  CALayer *sourceLayer = self.delegate.sourceImageView.layer;
  sourceLayer.rasterizationScale = 0.01;
  sourceLayer.shouldRasterize = YES;
  [sourceLayer renderInContext:ctx];
  CGContextSetShouldAntialias(ctx, NO);
  CGContextSetAllowsAntialiasing(ctx, NO);
  CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
}

预期的结果是图像将显示为块状/像素化位图。

有什么想法吗?谢谢!

编辑:添加了完整的drawRect代码,还尝试将Antialias函数立即移动到UIGraphicsGetCurrentContext行之后。

编辑:尝试#2(失败!)

- (void)drawRect:(CGRect)rect
{
  CGContextRef ctx = UIGraphicsGetCurrentContext();
  CGContextSetShouldAntialias(ctx, NO);
  CGContextSetAllowsAntialiasing(ctx, NO);
  CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
  CALayer *layer = [CALayer layer];
  layer.contents = (id)[UIImage imageNamed:@"test.jpg"].CGImage;
  layer.frame = CGRectMake(0, 0, 320, 411);
  layer.rasterizationScale = 0.0001;
  layer.shouldRasterize = YES;
  layer.geometryFlipped = NO;
  layer.edgeAntialiasingMask = 0;
  layer.minificationFilter = kCAFilterNearest;
  [layer renderInContext:ctx];
}

I want to get rid of any interpolation/antialiasing/etc when setting myLayer.rasterizationScale = 0.01 and myLayer.shouldRasterize = YES;

Example: enter image description here

Here's the code I'm trying:

- (void)drawRect:(CGRect)rect {
  CGContextRef ctx = UIGraphicsGetCurrentContext();
  CALayer *sourceLayer = self.delegate.sourceImageView.layer;
  sourceLayer.rasterizationScale = 0.01;
  sourceLayer.shouldRasterize = YES;
  [sourceLayer renderInContext:ctx];
  CGContextSetShouldAntialias(ctx, NO);
  CGContextSetAllowsAntialiasing(ctx, NO);
  CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
}

The expected result is that the image would display as a chunky/pixelated bitmap.

Any ideas? Thanks!

Edit: added full drawRect code, also tried moving the Antialias functions immediately following the UIGraphicsGetCurrentContext line.

Edit: Try #2 (fail!)

- (void)drawRect:(CGRect)rect
{
  CGContextRef ctx = UIGraphicsGetCurrentContext();
  CGContextSetShouldAntialias(ctx, NO);
  CGContextSetAllowsAntialiasing(ctx, NO);
  CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
  CALayer *layer = [CALayer layer];
  layer.contents = (id)[UIImage imageNamed:@"test.jpg"].CGImage;
  layer.frame = CGRectMake(0, 0, 320, 411);
  layer.rasterizationScale = 0.0001;
  layer.shouldRasterize = YES;
  layer.geometryFlipped = NO;
  layer.edgeAntialiasingMask = 0;
  layer.minificationFilter = kCAFilterNearest;
  [layer renderInContext:ctx];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光磨忆 2024-12-07 07:37:46

如果不进行插值,请将图层的 magnificationFilter 属性设置为 kCAFilterNearest。如果需要,还可以设置 minificationFilter。

此外,您不应在 -drawRect: 方法中设置图层的属性。相反,请在初始化视图或要更改图层内容时初始化图层属性。

For no interpolation, set the layer's magnificationFilter property to kCAFilterNearest. If desired, set the minificationFilter as well.

In addition, you shouldn't set up the layer's properties inside the -drawRect: method. Instead, initialize the layer properties when initializing the view, or when you want to change the layer content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文