在UIImage上画一个透明的圆圈-iPhone SDK
我在试图找出如何在UIImageView中的UIImage顶部绘制透明圆时遇到了很多麻烦。谷歌ing给了我一些线索,但我仍然找不到一个有效的例子 是否有任何人知道的例子可以证明这一点?一种方法是添加一个带有圆形的,直接添加到UIImageView的层,或者作为添加到UIImageView的新UIView的层 如果确实要修改图像,请将其绘制到CGBitmapContext中,然后从修改后的位图创建新图像,从而创建图像的可变副本在UIImage上画一个透明的圆圈-iPhone SDK,iphone,objective-c,uiimageview,Iphone,Objective C,Uiimageview,我在试图找出如何在UIImageView中的UIImage顶部绘制透明圆时遇到了很多麻烦。谷歌ing给了我一些线索,但我仍然找不到一个有效的例子 是否有任何人知道的例子可以证明这一点?一种方法是添加一个带有圆形的,直接添加到UIImageView的层,或者作为添加到UIImageView的新UIView的层 如果确实要修改图像,请将其绘制到CGBitmapContext中,然后从修改后的位图创建新图像,从而创建图像的可变副本 CGPathRef circlePath = CGPathCreate
CGPathRef circlePath = CGPathCreateMutable();
CGPathAddEllipseInRect( circlePath , NULL , CGRectMake( 0,0,20,20 ) );
CAShapeLayer *circle = [[CAShapeLayer alloc] init];
circle.path = circlePath;
circle.opacity = 0.5;
[myImageView.layer addSublayer:circle];
CGPathRelease( circlePath );
[circle release];
可以实现UIView的自定义子类,用于绘制图像,然后在drawRect方法中绘制圆: drawRect的实现:
- (void)drawRect:(CGRect)rect {
// first draw the image
[m_image drawInRect:m_viewRect blendMode:kCGBlendModeNormal alpha:1.0];
// then use quartz to draw the circle
CGContextRef context = UIGraphicsGetCurrentContext ()
// stroke and fill black with a 0.5 alpha
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 0.5);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.5);
// now draw the circle
CGContextFillEllipseInRect (context, m_viewRect);
}
您需要在init上设置m_viewRect和m_image成员函数。这必须是最简单的解决方案:
CGFloat r = 150;
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,1.5*r,1.5*r)];
lbl.text = @"●";
lbl.transform = CGAffineTransformMakeTranslation(0.0f, -r/6);
lbl.textAlignment = UITextAlignmentCenter;
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor redColor];
lbl.font = [UIFont systemFontOfSize:2*r];
lbl.alpha = 0.5;
lbl.center = self.view.center;
[self.view addSubview:lbl];
最简单的方法是创建一个半透明的方形UIView,然后将其层的拐角半径设置为其宽度/高度的一半。比如:
UIView *squareView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
squareView.alpha = 0.5;
squareView.layer.cornerRadius = 50;
...
[squareView release];
但是你怎么能看到一个透明的圆?它不是已经在那里了吗?我猜他指的是一个半透明的圆圈…@mvds你的评论,不管多么有趣,都不是很有帮助;)您忘记了squareView.clipsToBounds=是;没有它,这将不起作用。只有当你想在它上面添加另一个视图时,这才是另一回事。我也很抱歉,但我喜欢它!
UIView *squareView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)];
squareView.alpha = 0.5;
squareView.layer.cornerRadius = 50;
...
[squareView release];