如何在UIImageView
蒙版图像上放置阴影?
我不是指矩形阴影——我也想对阴影应用相同的蒙版效果。
给 UIImageView 提供阴影效果试试下面的代码..
1) #import <QuartzCore/QuartzCore.h> in .h file
2) To give a shadow effect to Cell's UIImageView
mediaImage.layer.shadowColor = [UIColor blackColor].CGColor;
mediaImage.layer.shadowRadius = 10.f;
mediaImage.layer.shadowOffset = CGSizeMake(0.f, 5.f);
mediaImage.layer.shadowOpacity = 1.f;
mediaImage.clipsToBounds = NO;
出色地!你可以试试这个。
// Use a White background to make the shadow prominent.
self.view.backgroundColor = [UIColor whiteColor];
// The image we're going to mask and shadow
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourImage.jpeg"]];
image.center = self.view.center;
// Make new layer to contain shadow and masked image
CALayer *containerLayer = [CALayer layer];
containerLayer.shadowColor = [UIColor blackColor].CGColor;
containerLayer.shadowRadius = 10.f;
containerLayer.shadowOffset = CGSizeMake(0.f, 5.f);
containerLayer.shadowOpacity = 1.f;
// Use the image's layer to mask the image into a circle
image.layer.cornerRadius = roundf(image.frame.size.width/2.0);
image.layer.masksToBounds = YES;
// Add masked image layer into container layer so that it's shadowed
[containerLayer addSublayer:image.layer];
// Add container including masked image and shadow into view
[self.view.layer addSublayer:containerLayer];
您可以自定义一个名为 NEWImageVIew 的子类继承 UIImageView。在 NEWImageVIew 中您可以创建一个名为 realImageContainer 的属性,您可以将图像设置为该属性。
@interface NEWImageView : UIImageView
@property (nonatomic,strong) UIImageView *realImageContainer;
@end
@implementation NEWImageView
- (UIImageView *)realImageContainer {
if (!_realImageContainer) {
_realImageContainer = [UIImageView new];
[self addSubview:_realImageContainer];
[_realImageContainer mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
}
return _realImageContainer;
}
@end
当你想对图像进行蒙版和阴影处理时,可以在realImageContainer中设置图像,并将蒙版设置为realImageContainer层,这样就得到了蒙版层。
NEWImageView *newImgView = [[NEWImageView alloc] init];
newImgView.backgroundColor = [UIColor clearColor];
newImgView.contentMode = UIViewContentModeScaleAspectFit;
newImgView.realImageContainer.image = self.image;
newImgView.realImageContainer.layer.mask = self.maskLayer;//this masklayer you can make youself
阴影呢?因为newImageView的view中添加了realImageContainer,NEWImageView的layer还没有使用,可以在这里设置阴影。
newImgView.layer.shadowColor = [UIColor blackColor].CGColor;
newImgView.layer.shadowOpacity = 0.33;
newImgView.layer.shadowRadius = 8;
newImgView.layer.shadowOffset = CGSizeMake(0, 19);
您必须设置具有透明背景的图像,然后像这样添加阴影:
imageView.layer.shadowColor = [UIColor blackColor].CGColor;
imageView.layer.shadowOpacity = 0.1;
imageView.layer.shadowRadius = 5;
imageView.layer.shadowOffset = CGSizeMake(5, 5);
[imageView setClipsToBounds:NO];
记住你必须导入 QuarzCore 库