iOS SDK - 在蒙版图像上投下阴影

时间:2013-11-03 22:06:54

标签: ios uiimageview

如何在UIImageView上放置带有蒙版图像的阴影?

我不是指一个长方形的阴影 - 我也想对阴影应用相同的蒙版效果。

4 个答案:

答案 0 :(得分:4)

将阴影效果赋予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;

答案 1 :(得分:1)

嘛!你可以尝试这个。

// 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];

答案 2 :(得分:0)

你必须设置一个透明背景的图像,然后你添加阴影:

    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库

答案 3 :(得分:0)

您可以自定义一个名为NEWImageVIew的子类继承UIImageView.In 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中设置图像并将mask设置为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

阴影怎么样?因为realImageContainer被添加到NEWImageView的视图中,NEWImageView的图层尚未使用,你可以在这里设置阴影。

newImgView.layer.shadowColor = [UIColor blackColor].CGColor;
newImgView.layer.shadowOpacity = 0.33;
newImgView.layer.shadowRadius = 8;
newImgView.layer.shadowOffset = CGSizeMake(0, 19);

所以你得到一个蒙面和阴影的图像。(我使用三角形图像作为掩模,所以我得到这个图像) enter image description here