为所有 UIImageView 添加圆角
我想为我的项目中的所有 UIImageView 添加一些圆角。我已经让代码正常工作,但我必须将其应用到每个图像;我应该子类化 UIImageView 来添加它吗?如果是这样,有人可以给我一些关于如何做到这一点的指示吗?
这是代码
- (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;
}
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?
Here is the code
- (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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
检查这个 -
UIImage 上的圆角
图层修改似乎是最好的方法。
Check this -
Rounded Corners on UIImage
The layer modification seems to be the best way.
您可以使用 UIImage 的类别,这是子类化类的另一种方法,有时只需进行小的更改就更容易。
例如,添加一个返回带有圆角属性集的 UIImage 的方法。
有关 Objective-c 类别的更多信息,请访问 http://macdevelopertips.com/objective -c/objective-c-categories.html
You could use a category for UIImage which is an alternate way to subclass a Class and sometimes easier for just small changes.
e.g add a method that returns a UIImage with the rounded corner attributes set.
more info on Objective-c categories can be found http://macdevelopertips.com/objective-c/objective-c-categories.html
您可以通过 UIImageView 和 CALayer 上的简单类别来实现更强大的功能,而不是子类化。
在 UIImageView 上创建一个类别,如下所示:
这会调用 CALayer 上的类别方法:
因此,这允许您对角的任意组合(参见
UIRectCorner
)进行圆化,如果您想放置图像,这尤其方便在组样式UITableView
中。然而,这样做时有一个警告。因为我们没有子类化UIImageView
,所以我们无法将任何代码注入到layoutSubviews
中,这意味着遮罩层可能不正确。事实上,在配置单元格时,当您调用类别方法时,甚至不会设置图像视图的边界。因此,您需要确保在添加圆角之前设置图像视图的边界(除非使用UIRectCornersAllCorners
)。以下是执行此操作的一些代码:
我有另一个删除圆角的类别 - 所做的就是删除任何遮罩并将
cornerRadius
设置为 0。Rather than subclassing, you can achieve more powerful functionality through simple categories on UIImageView and CALayer.
Create a category on UIImageView like this:
This calls a category method on CALayer:
So, this allows you to round any combination (see
UIRectCorner
) of corners, which is especially handy if you want to put an image in a group styleUITableView
. There is one caveat when doing this however. Because we've not subclassedUIImageView
, we cannot inject any code intolayoutSubviews
, which means that the mask layer may not be correct. In fact, when configuring cells, the bounds of the image view won't even be set when you call the category method. Hence, you need to ensure the bounds of the image view is set before adding rounded corners (except if usingUIRectCornersAllCorners
).Here is some code which does this:
I have another category which removes rounded corners - all that does is remove any masks and set the
cornerRadius
to 0.是的,您应该子类化 UIImageView,并在整个项目中使用自定义子类。
Yes, you should subclass UIImageView, and use your custom subclass throughout your project.
您可以子类化 UIImageView,然后如果您实现其 setNeedsDisplay 方法,圆角将在子类上工作。 (不要忘记导入QuartzCore)
You can subclass UIImageView and then if you implement its setNeedsDisplay method the round corners will work on subclasses. (don't forget to import QuartzCore)
试试这个,
这可能对你有帮助。
Try this,
this may help you.