将圆角添加到UIImageVIew并显示阴影效果 [英] Add Round Corner to UIImageVIew and Display Shadow effect

查看:304
本文介绍了将圆角添加到UIImageVIew并显示阴影效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和这里的链接有同样的问题:
不能添加圆角半径和阴影

I have the same problem as the link here: Can't add a corner radius and a shadow

如果我放置maskToBounds = YES,则得到圆角,但是没有阴影
如果我把maskToBounds = NO,我得到了阴影,但是没有圆角。

if I put maskToBounds = YES, I get round corners, but no shadow If I put maskToBounds = NO, I get shadow, but no round corners.

然后,我按照上面链接中的说明进行操作,设置maskToBounds = NO,,而是设置圆角半径并使用圆角rect设置阴影的贝塞尔曲线路径。保持两个半径相同。但是然后,我没有弯角,也没有阴影! (即没有阴影的正方形图像)您能帮我这个忙吗?我不知道我做错了什么。提前致谢。

Then I followed the instruction in that link above , setting maskToBounds = NO, " but rather set the corner radius and set the bezier path of the shadow with a rounded rect. Keep the radius of the two the same ". But then, I don't get round corners, nor did I get any shadow! (i.e. Square Image without Shadow) Could you please help me out of this? I don't know what did I do wrong. Thanks in advance.

self.userImageView.backgroundColor = [UIColor redColor]; 
self.userImageView.clipsToBounds = NO; 
self.userImageView.contentMode = UIViewContentModeCenter; 
self.userImageView.layer.masksToBounds = NO; 

self.userImageView.layer.borderWidth = 1; 
self.userImageView.layer.borderColor = [[UIColor grayColor] CGColor]; 

self.userImageView.layer.shadowOpacity = 1; 
self.userImageView.layer.shadowColor = [[UIColor blackColor] CGColor]; 
self.userImageView.layer.shadowRadius = 8.0f; 
self.userImageView.layer.shadowOffset = CGSizeMake(-3, 0);
self.userImageView.layer.shouldRasterize = YES; 

self.userImageView.layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:[self.userImageView bounds] cornerRadius:10.0f] CGPath]; 

[self addSubview:self.userImageView]; 


推荐答案

不幸的是,我不认为UIImageView支持圆角

Unfortunately, I don't think UIImageView supports rounded corner and shadow at the same time.

但是,您可以在UIImageView的超级视图中创建阴影。

You can, however, make the shadow in the UIImageView's super view.

CGFloat cornerRadius = 3.0

UIView *container = [[UIView alloc] initWithFrame:aRect];
container.layer.shadowOffset = CGSizeMake(0, 0);
container.layer.shadowOpacity = 0.8;
container.layer.shadowRadius = 5.0;
container.layer.shadowColor = [UIColor redColor].CGColor;
container.layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:container.bounds cornerRadius:cornerRadius] CGPath];

self.userImageView.layer.cornerRadius = cornerRadius;
self.userImageView.layer.masksToBounds = YES;
self.userImageView.frame = container.bounds;
[container addSubview:self.userImageView];
[self addSubview:container];

这篇关于将圆角添加到UIImageVIew并显示阴影效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆