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

查看:19
本文介绍了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.

有什么想法吗?

提前致谢.

推荐答案

如果您在 Retina 设备上进行开发,问题可能与您的图形上下文的分辨率有关.你会尝试:

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天全站免登陆