2012-07-05 44 views
0

我創建了myView,它是UIView的子類,並在myView上繪製了一個圓。現在我想在圓圈內顯示小圖像,而不是在圓圈外。但是我在圈子和圈子外面,這意味着圖像顯示在整個myVIew。如何在iOS中的圖中顯示小圖像

我得到了像如下圖片作爲

enter image description here

但我想獲得如下的

enter image description here

是否有可能或不?請幫幫我。

回答

2

假設橢圓是UIBezierPath,您可以使用:使用圖形圖象創建

UIImage *patternImage = [UIImage imageNamed:@"thePattern.png"]; 
UIColor *fillPattern = [UIColor colorWithPatternImage:patternImage]; 
[fillPattern setFill]; 
[thePath fill]; 

編輯

填充的橢圓由CGContextAddEllipseInRect

- (void)drawRect:(CGRect)rect { 

    CGImageRef patternImage = [UIImage imageNamed:@"file"].CGImage; 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextAddEllipseInRect(context, CGRectMake(100, 100, 200, 200)); 
    CGContextClip(context); 
    CGContextDrawTiledImage(context, CGRectMake(20, 20, 48, 36),patternImage); 

} 
+0

謝謝重播。我沒有使用UIBezierPath。我使用了CGContextAddEllipseInRect方法。請建議我。 – 2012-07-05 12:38:38

+0

添加了其他示例。 – Anne 2012-07-05 13:24:54

+0

非常感謝。我幾乎已經按照你寫的那樣嘗試過了,但是沒有使用CGContextClip。這就是爲什麼我沒有得到它。最後,我得到了它,因爲你的幫助。 – 2012-07-05 13:37:30

1

你可以試試這個:

#import <QuartzCore/QuartzCore.h>

,並在你的代碼,使圈後,添加圖像,並設置這些: myImageView.layer.cornerRadius = x;

myImageView.layer.masksToBounds = TRUE;

這可以讓你有你的形象圓角。如果您計算半徑以匹配您的圈子,則應該獲得所需的外觀。

希望這會有所幫助。

乾杯,

喬治

+0

謝謝喬治。我知道了。但它是圓的工作,而不是三角形,五邊形。 – 2012-07-05 13:21:05

相關問題