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:平滑图像
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01