Search code examples
iphoneioscore-graphicsiphone-4

CGContextDrawImage in Retina draws image pixelated?


I need to draw images into a CALayer because I need to perform various effects, animations and filters on it. When I do simple drawing into the CGContext no matter what I do it always gets drawn pixelated... What's the right way to draw onto a context in retina?

This is what I'm doing now:

CGImageRef plateImage = [[UIImage imageNamed:@"someImage"] CGImage];
CGFloat width = CGImageGetWidth(plateImage), height = CGImageGetHeight(plateImage);
CGFloat scale = [[UIScreen mainScreen] scale];

NSLog(@"Scale: %f\nWidth: %f\nHeight: %f", scale, width, height);
CGContextTranslateCTM(_context, 0, height / scale);
CGContextScaleCTM(_context, 1.0, -1.0);

CGContextDrawImage(_context, CGRectMake(0, 0, width / scale, height / scale), plateImage);

Solution

  • You need to set the contents scale of the layer appropriately.

    myLayer.contentsScale = [UIScreen mainScreen].scale