UIImage的drawInrect:平滑图像

我试图使用UIImagedrawInRect:方法来绘制图像。 这里是代码:

 UIImage *image = [UIImage imageNamed:@"OrangeBadge.png"]; UIGraphicsBeginImageContext(image.size); [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)]; UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 

问题是由此产生的图像模糊。 这是最后的图像(在右侧)与源图像(在左侧)的比较:

源图像和模糊的图像

我试过CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), NO) CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), NO)但是这并没有解决问题。

有任何想法吗?

提前致谢。

如果您正在使用视网膜设备进行开发,可能问题与graphics上下文的分辨率有关。 你会尝试与:

 UIGraphicsBeginImageContextWithOptions(size, NO, 2.0f); 

这将使视网膜parsing。 另外,你的图片应该在@ 2x分辨率下才能使用。

如果您还想支持非视网膜设备,您可以使用:

 if ([UIScreen instancesRespondToSelector:@selector(scale)]) { UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0f); } else { UIGraphicsBeginImageContext(newSize); } 
    Interesting Posts