【发布时间】:2015-02-23 05:16:05
【问题描述】:
我正在尝试创建一个自定义视图来绘制一张音乐 CD,其中包含 CD 上的专辑插图。
目前我使用核心图形在drawRect方法内绘制两个圆盘。内圆盘的颜色设置为视图的背景色,使内圆看起来像一个空心圆。
我的问题是我无法在圆形贝塞尔路径内绘制 UIImage 以获得我需要的结果。
这是我的 drawRect 代码:
// Draw a CD like view using three drawing operations:
// 1st draw the main disk
// 2nd draw the image if it is set
// 3rd draw the inner disk over the image so that it looks like it is a hollow disk with the image painted on the disk.
// Note:) This view expects its aspect ratio to be set as 1:1
- (void)drawRect:(CGRect)rect
{
// Draw the main Circular CD disk
UIBezierPath* outerRing = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
[self.mainColor setFill];
[outerRing fill];
[self.outerRingColor setStroke];
[outerRing stroke];
// Draw the album image if it is set
if(self.artwork){
[self.artwork drawInRect:self.bounds blendMode:kCGBlendModeNormal alpha:1.0];
}
// now draw another smaller disk inside
// this will be a percentage smaller than the whole disk's bounds and will be centered inside it
CGFloat sidePaddingCoord = ((1.0f - INNER_DISK_SIZE_PERCENTAGE) / 2) * self.bounds.size.height;
CGFloat side = INNER_DISK_SIZE_PERCENTAGE * self.bounds.size.width;
UIBezierPath* innerRing = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(sidePaddingCoord, sidePaddingCoord, side, side)];
[self.innerRingColor setFill];
[innerRing fill];
[innerRing stroke];
}
这会将图像填充为正方形。我希望将图像剪辑到 outerRing 贝塞尔路径 内,使其看起来像一张音乐 CD。
我确信除了使用图像的 drawInRect 方法之外,还有一种方法可以使用核心图形来实现其他目的。有没有办法在圆形贝塞尔路径内“剪辑”图像或仅在贝塞尔路径内绘制?
【问题讨论】:
标签: ios objective-c core-graphics