UIImage的drawInrect:平滑图像 [英] UIImage's drawInrect: smoothes image

查看:400
本文介绍了UIImage的drawInrect:平滑图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用UIImagedrawInRect:方法绘制图像.这是代码:

I'm trying to draw image using UIImage's drawInRect: method. Here is the code:

UIImage *image = [UIImage imageNamed:@"OrangeBadge.png"];

UIGraphicsBeginImageContext(image.size);

[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];

UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

问题是生成的图像模糊.这是生成的图像(在右侧)与源图像(在左侧)相比:

The problem is that the resulting image is blurry. Here is the resulting image (on the right side) compared to the source image (on the left side):

我都尝试过CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), NO) CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), NO),但这不能解决问题.

I've tried both CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), NO) CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), NO) but this did not solve the problem.

有什么想法吗?

谢谢.

推荐答案

如果在视网膜设备上进行开发,则问题可能与图形上下文的分辨率有关.您可以尝试:

If you are developing on a retina device, possibly the issue is related to the resolution of your graphics context. Would you try with:

UIGraphicsBeginImageContextWithOptions(size, NO, 2.0f);

这将启用视网膜分辨率.另外,您的图片应该以@ 2x分辨率可用,以使其正常工作.

This will enable retina resolution. Also, your image should be available at @2x resolution for this to work.

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

If you want to support non-retina devices as well, you can use:

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

这篇关于UIImage的drawInrect:平滑图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆