2013-04-08 49 views
7

我正在Xamarin开发,并希望绘制一个简单的圆圈内的文字“CIRCLE”,并在UIImageView中显示该图像。问题在于圆圈和文字显得非常模糊。我读了一些关于子像素的内容,但我不认为这是我的问题。iOS绘制一个简单的图像变成模糊使用Xamarin C#

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

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; 

Blurry Circle

回答

12

为什么它是模糊的原因是因为它被调整。该位图不是150x150(就像您创建的上下文一样),它的尺寸为333x317像素。

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

UIGraphics.BeginImageContextWithOptions (size, false, 0); 

将(使用0)自动地采用定标因子视网膜(或不)正确的显示 - 看看脆(而不是过大),对所有类型的设备。

+0

像素加倍视网膜是问题,你的建议解决了它。良好的工作,谢谢:) – 2013-04-09 08:54:38