有什么方法可以在objective-c/iPhone/iOS SDK 中禁用CALayer 的rasterizationScale 插值/抗锯齿?

tab*_*ber 2 core-graphics calayer quartz-graphics quartz-2d ios4

我想在设置myLayer.rasterizationScale = 0.01和设置时摆脱任何插值/抗锯齿/等myLayer.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);
}
Run Code Online (Sandbox Code Playgroud)

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

有任何想法吗?谢谢!

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

编辑:尝试#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];
}
Run Code Online (Sandbox Code Playgroud)

Kev*_*ard 5

对于无插值,将图层的 magnificationFilter 属性设置为 kCAFilterNearest。如果需要,还可以设置 minificationFilter。

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