2013-04-08 25 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