2013-05-30 37 views
1

我使用以下帶邊框的代碼繪製了一個橢圓。當我使用CGContextDrawImage方法時,我沒有獲得邊框,圖像可見爲橢圓形狀。否則,我得到這個橢圓的邊界線。其實我想獲得與橢圓形狀和邊界的圖像。我只有其中一個。我想得到兩個。如何在ios 6.0中使用CGContextDrawImage獲得橢圓的邊界

- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
    context =UIGraphicsGetCurrentContext(); 
CGContextClearRect(context, self.bounds); 
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); 
CGContextSetLineWidth(context, 5.0); 
CGContextAddEllipseInRect(context, self.bounds); 

CGContextClip(context); 
CGContextStrokePath(context); 
    CGContextDrawImage(context, displayImageView.frame, displayImageView.image.CGImage); 

} 

編輯:

我怎麼能解決我的問題,下面的圖片爲
enter image description here

上述觀點有白色邊界圖像。我想要這樣。請告訴我任何一個。

+0

你爲什麼這樣做'如果(displayImageView.image!)'?我在這裏錯過了什麼.. – Mar0ux

+0

對不起。我編輯過。 –

+0

有幾件事情 - 'CGContextDrawPath'應該清除當前路徑。所以'CGContextIsPathEmpty(context)'應該返回'true';你能證實這一點嗎?如果是這樣,那就不會有裁剪。另外,你想要在圓圈外面還是在圓圈內?在外面的情況下,你應該在裁剪之前先行程,如果是內側,你應該在裁剪之後對其進行裁剪。 – Mar0ux

回答

3

CGContextClip也復位電流路徑:

確定新的剪切路徑後,功能復位 上下文的當前路徑設置爲空路徑。

這樣做:

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextClearRect(context, self.bounds); 
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); 
CGContextSetLineWidth(context, 5.0); 
CGContextAddEllipseInRect(context, self.bounds); 

CGContextClip(context); 
CGContextDrawImage(context, displayImageView.frame, displayImageView.image.CGImage); 
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor); 
CGContextSetLineWidth(context, 5.0); 
CGContextAddEllipseInRect(context, self.bounds); 
CGContextStrokePath(context);