有什么方法可以在 Objective-C / iPhone / iOS SDK 中禁用 CALayer 的 rasterizationScale 插值/抗锯齿功能吗?
我想在设置 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);
}
预期的结果是图像将显示为块状/像素化位图。
有什么想法吗?谢谢!
编辑:添加了完整的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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不进行插值,请将图层的 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.