2017-01-04 17 views
0

我想在UIImageView上畫一個圓圈。我已經嘗試過,但沒有奏效。如何在UIImageView上繪製圓而不在objective-c中添加圖層?

這是我所希望實現示例圖像:

圓圈應該在哪裏用戶點擊UIImageView繪製,我想這樣做,不添加任何sublayer

這是否有辦法做到這一點? 到目前爲止,我從互聯網上使用這段代碼,但它沒有奏效。

- (UIImage *)imageByDrawingCircleOnImage:(UIImage *)image 
            pointX:(float) x 
           PointY:(float) y 
{ 
    // begin a graphics context of sufficient size 
    UIGraphicsBeginImageContext(image.size); 

    // draw original image into the context 
    [image drawAtPoint:CGPointZero]; 

    // get the context for CoreGraphics 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    // set stroking color and draw circle 
    [[UIColor redColor] setStroke]; 

    // make circle rect 5 px from border 
    CGRect circleRect = CGRectMake(0, 0, 
            image.size.width, 
            image.size.height); 
    circleRect = CGRectInset(circleRect, x, y); 

    // draw circle 
    CGContextStrokeEllipseInRect(ctx, circleRect); 

    // make image out of bitmap context 
    UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext(); 

    // free the context 
    UIGraphicsEndImageContext(); 
    return retImage; 
} 
+3

你能分享你已經嘗試了什麼至今? – lsouza

+0

不知何故,我不認爲你正在爲iOS 4構建。請正確標記。 – rmaddy

+0

給角半徑的一半的寬度 –

回答

0

假設你的目標的看法是廣場,設置cornerRadius的寬度或高度的一半。 maskToBounds設置你的形象爲每圓潤imageView

例如形狀,添加此代碼爲您的要求,

yourImageView.layer.cornerRadius = yourImageView.imageView.frame.size.height /2; 
yourImageView.layer.masksToBounds = YES; 

希望這將幫助你:)

+0

圖像加載在圖像視圖我想畫繞過小半徑的圖像。 – sohaib

+0

閱讀本文,它會幫助你:https://agilewarrior.wordpress.com/2012/09/04/how-to-draw-circles-where-ever-someone-taps-your-screen-iphone-ipad-ios / –

相關問題