为所有UIImageViews添加圆角 [英] Add rounded corners to all UIImageViews
问题描述
我想为项目中的所有UIImageView添加一些圆角。我已经有了代码工作,但我必须将它应用于每个图像;我应该继承UIImageView来添加它吗?如果有的话,有人可以给我一些关于如何做的指示吗?
I would like to add some rounded corners to all of the UIImageViews in my project. I have already got the code working, but am having to apply it to every image; should I subclass UIImageView to add this? If so, can someone give me some pointers as to how to do this?
这是代码
- (void)viewDidLoad {
[super viewDidLoad];
NSString *mainpath = [[NSBundle mainBundle] bundlePath];
welcomeImageView.image = [UIImage imageWithContentsOfFile:[mainpath stringByAppendingString:@"/test.png"]];
welcomeImageView.layer.cornerRadius = 9.0;
welcomeImageView.layer.masksToBounds = YES;
welcomeImageView.layer.borderColor = [UIColor blackColor].CGColor;
welcomeImageView.layer.borderWidth = 3.0;
CGRect frame = welcomeImageView.frame;
frame.size.width = 100;
frame.size.height = 100;
welcomeImageView.frame = frame;
}
推荐答案
您可以使用类别UIImage是一种子类化的替代方法,有时候只是很小的更改。
You could use a category for UIImage which is an alternate way to subclass a Class and sometimes easier for just small changes.
例如,添加一个返回带有圆角属性的UIImage的方法。
e.g add a method that returns a UIImage with the rounded corner attributes set.
+(UIImage *)imageWithContentsOfFile:(NSString *)file cornerRadius:(NSInteger)...
有关Objective-c类别的更多信息,请参阅 http://macdevelopertips.com/objective-c/objective-c-categories.html
more info on Objective-c categories can be found http://macdevelopertips.com/objective-c/objective-c-categories.html
这篇关于为所有UIImageViews添加圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!