Jak*_* T. 0 objective-c shadow uiimageview ios
我知道有类似的问题,但没有找到帮助我解决我的问题的答案.如果您已经看过一个,请随意将其标记为重复,但请确保它回答整个问题,而不仅仅是类似的部分.
我有一个UIImageView,我正在修改角落并为其添加边框.但是,我开始添加一些阴影以给我的UI一些深度.我正在为我的个人资料图片添加一个阴影,以下代码非常适合一个完美的方形图像,它会被裁剪成一个完美的圆圈.但是,对于非方形矩形图像,阴影应用于图像本身而不是图像视图,因此我最终在裁剪的矩形图像周围出现圆形边框,其边缘周围有阴影,无法到达圆形边界.有关如何修改我的代码以将此阴影应用于边界而不是UIImageView中的图像的任何见解?
谢谢
//Set profile image view to be rounded (default appears rounded due to alpha background, need rest to appear rounded as well
[self.profileImageView layoutIfNeeded];
CALayer *imageLayer = self.profileImageView.layer;
[imageLayer setCornerRadius:self.profileImageView.frame.size.width/2];
[imageLayer setBorderWidth:1];
[imageLayer setBorderColor:[[UIColor colorWithRed:78.0/255.0 green:82.0/255.0 blue:85.0/255.0 alpha:1] CGColor] ];
[imageLayer setMasksToBounds:YES];
//Apply shadows to necessary views for the job badge
[self.profileImageView.layer setShadowColor:[[UIColor blackColor] CGColor]];
[self.profileImageView.layer setShadowRadius:4.0f];
[self.profileImageView.layer setShadowOffset:CGSizeMake(0, -3)];
[self.profileImageView.layer setShadowOpacity:0.5f];
Run Code Online (Sandbox Code Playgroud)
问题是在设置时阴影会与其他内容一起屏蔽cornerRadius.
您可以使用带有阴影和蒙版imageView的中间竞争视图.这是代码,但您也可以使用Interface Builder和自动布局执行相同的操作:
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
[containerView.layer setShadowColor:[[UIColor blackColor] CGColor]];
[containerView.layer setShadowRadius:4.0f];
[containerView.layer setShadowOffset:CGSizeMake(0, -3)];
[containerView.layer setShadowOpacity:0.5f];
[self.view addSubview:containerView];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:containerView.bounds];
imageView.image = [UIImage imageNamed:@"rgb"];
[imageView.layer setCornerRadius:imageView.frame.size.width/2];
[imageView.layer setBorderWidth:1];
[imageView.layer setBorderColor:[[UIColor colorWithRed:78.0/255.0 green:82.0/255.0 blue:85.0/255.0 alpha:1] CGColor] ];
[imageView.layer setMasksToBounds:YES];
[containerView addSubview:imageView];
Run Code Online (Sandbox Code Playgroud)
输出: