UIImage#39;s drawInrect: smoothes image(UIImage 的 drawInrect:平滑图像)
问题描述
我正在尝试使用 UIImage
的 drawInRect:
方法绘制图像.代码如下:
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:平滑图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIImage 的 drawInrect:平滑图像


基础教程推荐
- NSString intValue 不能用于检索电话号码 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01