【问题标题】:IOS drawing a simple image turns out blurry using Xamarin C#IOS 使用 Xamarin C# 绘制一个简单的图像变得模糊
【发布时间】:2013-03-31 04:21:28
【问题描述】:

我正在 Xamarin 中进行开发,并想在其中绘制一个带有文本“CIRCLE”的简单圆圈,并在 UIImageView 中显示该图像。问题是圆圈和文字显得非常模糊。我读过一些关于亚像素的文章,但我认为这不是我的问题。

这是模糊的图像和代码,希望有人有一些想法:)

UIGraphics.BeginImageContext (new SizeF(150,150));
var context = UIGraphics.GetCurrentContext ();

var content = "CIRCLE";
var font = UIFont.SystemFontOfSize (16);

const float width = 150;
const float height = 150;

context.SetFillColorWithColor (UIColor.Red.CGColor);
context.FillEllipseInRect (new RectangleF (0, 0, width, height));

var contentString = new NSString (content);
var contentSize = contentString.StringSize (font);

var rect = new RectangleF (0, ((height - contentSize.Height) / 2) + 0, width, contentSize.Height);

context.SetFillColorWithColor (UIColor.White.CGColor);
new NSString (content).DrawString (rect, font, UILineBreakMode.WordWrap, UITextAlignment.Center);

var image = UIGraphics.GetImageFromCurrentImageContext ();
imageView.Image = image;

【问题讨论】:

    标签: ios xamarin.ios blurry uigraphicscontext


    【解决方案1】:

    它模糊的原因是它被调整了大小。该位图不是 150x150(如您创建的上下文),而是 333x317 像素。

    这可能是因为imageView 正在缩放它的图像(没有源代码)或因为像素加倍(对于视网膜显示器)。在后一种情况下,您真正​​想要使用的是:

    UIGraphics.BeginImageContextWithOptions (size, false, 0);
    

    它将(使用0)自动为视网膜(或不)显示使用正确的缩放因子 - 并且在所有类型的设备上看起来清晰(而不是过大)。

    【讨论】:

    • 视网膜的像素加倍是问题所在,您的建议解决了它。干得好,谢谢 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    相关资源
    最近更新 更多