Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 948
shein Avatar asked Feb 25 '12 16:02

shein


1 Answers

I had the same problem but the solution didn't seem to work.

UIGraphicsBeginImageContext() turned out to be causing my problem. I'm posting my solution here for future users with the same problem.

From iOS 4.0 you should use:

UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);

instead of

UIGraphicsBeginImageContext(size);

If you don't want pixelated images.

like image 59
Tieme Avatar answered Oct 23 '22 13:10

Tieme