2011-12-29 63 views
8

我需要将圆角添加到UIImageView。我从论坛中找到了一个解决方案,并且我尝试了下面的代码;圆角UIView或UIImageView

UIImageView * roundedView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"wood.jpg"]]; 
// Get the Layer of any view 
CALayer * l = [roundedView layer]; 
[l setMasksToBounds:YES]; 
[l setCornerRadius:10.0]; 

我使用的是iOS 5,以及setMasksToBounds:YESsetCornerRadius都没有发现。

那么有什么其他方法可以在我的UIImageview中找到圆角吗?

+0

您是否尝试将'l'分配回'roundedView'的图层属性? – JoePasq 2011-12-29 15:16:29

+0

怎么样?能告诉我它会是什么样子? – Illep 2011-12-29 15:25:45

+0

'roundedView.layer = l;'我还没有尝试过。 – JoePasq 2011-12-29 15:54:47

回答

14

要在UIView(或它的子类UIImageView)圆角,你需要的,就像你在你的问题中写道,设置cornerRadius图层上。例如:

theRoundedCornersView.layer.cornerRadius = 10.0f; 

导入正确的标题,它会编译:

#import <QuartzCore/QuartzCore.h> 

不要忘了将它添加到您的框架反对链接。

4
#import <QuartzCore/QuartzCore.h> 

,并连接QuartzCore

1

此行添加到您的.h文件:

#import <QuartzCore/QuartzCore.h> 

...并警告将会消失(代码仍然没有进口工作)。您的问题与iOS 5无关。