Retina中的CGContextDrawImage绘制图像像素化?

she*_*ein 11 iphone core-graphics ios iphone-4

我需要将图像绘制到CALayer中,因为我需要对其执行各种效果,动画和过滤器.当我简单地绘制到CGContext时,无论我做什么,它总是被绘制像素化...什么是在视网膜上绘制上下文的正确方法?

这就是我现在正在做的事情:

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);
Run Code Online (Sandbox Code Playgroud)

Tie*_*eme 25

我遇到了同样的问题,但解决方案似乎没有用.

UIGraphicsBeginImageContext()原来是导致我的问题.我在这里发布我的解决方案,为未来的用户提出同样的问题.

从iOS 4.0开始,你应该使用:

UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
Run Code Online (Sandbox Code Playgroud)

代替

UIGraphicsBeginImageContext(size);
Run Code Online (Sandbox Code Playgroud)

如果您不想要像素化图像.

  • 值得注意的是,我将最佳结果与已接受的答案相结合:`UIGraphicsBeginImageContextWithOptions(imageSize,NO,[UIScreen mainScreen] .scale);` (2认同)

Eri*_*ner 19

您需要适当地设置图层的内容比例.

myLayer.contentsScale = [UIScreen mainScreen].scale
Run Code Online (Sandbox Code Playgroud)