2

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];
}
taber
  • 3,166
  • 4
  • 46
  • 72

1 Answers1

5

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.

Kevin Packard
  • 374
  • 4
  • 13
  • 1
    Bummed out that the accepted answer is incorrect, while my answer is correct - especially considering my lack of points and the obscurity of the question. – Kevin Packard Dec 09 '16 at 19:01
  • 1
    apologies; your answer is definitely correct, and if I were permitted to delete mine then I would. Alas, a vote is all I can give. – Tommy Jan 15 '19 at 02:16
  • 1
    @KevinPackard fixed, sorry it took me almost 4 years. – taber Jan 15 '19 at 05:05