2012-01-29 27 views
0

我在自定義UIButton內使用drawRect繪製了一個帶有圖像的邊框按鈕。代碼如下:IOS:drawRect在角落顯示1px

- (void) drawRect:(CGRect)rect { 
CGContextRef context = UIGraphicsGetCurrentContext(); 

//Draw a rectangle 
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor); 
CGContextSetStrokeColorWithColor(context, [[UIColor grayColor] CGColor]); 
//Define a rectangle 
CGRect drawrect = CGRectMake(CGRectGetMinX(rect),CGRectGetMinY(rect),rect.size.width,rect.size.height); 

CGContextStrokeRect(context,drawrect);} 

問題是,在角落我得到了一個額外的像素(見附圖)。我究竟做錯了什麼?

enter image description here

感謝

+2

「我在做什麼錯?」 - 你正在調整圖像的大小以至於看不到它:) – 2012-01-29 20:14:46

回答

6

你沿着像素的邊緣繪製的,而不是沿像素的中心,它繪製您的矩形。所以你的矩形只覆蓋大部分像素的一半。在角落它涵蓋了四分之三的像素。

要沿像素中心繪製,必須使用半整數座標。試試這個:

CGContextStrokeRect(context, CGRectInset(rect, 0.5, 0.5));