Cli*_*ead 9 blurry xamarin.ios ios uigraphicscontext
我正在使用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;
Run Code Online (Sandbox Code Playgroud)

pou*_*pou 15
它之所以模糊,是因为它被调整了大小.该位图不是150x150(就像你创建的上下文一样),它是333x317像素.
这可能是因为imageView缩放它的图像(没有源代码)或因为像素倍增(用于视网膜显示).在后一种情况下,您真正想要使用的是:
UIGraphics.BeginImageContextWithOptions (size, false, 0);
Run Code Online (Sandbox Code Playgroud)
这将(使用0)自动使用正确的视网膜(或不显示)缩放因子 - 并在所有类型的设备上看起来清脆(而不是超大).