IOS SDK - Shadows on a masked image

How to cast a shadow on UIImageViewthat has a hidden image?

I do not mean a rectangular shadow - I would also like to apply the same mask effect to the shadow.

+4
source share
3 answers

To give a shadow effect to UIImageView Try entering the code below.

1) #import <QuartzCore/QuartzCore.h> in .h file

2) To give a shadow effect to Cell 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;
+4
source

you need to set the image with a transparent background and then add a shadow like this:

    imageView.layer.shadowColor = [UIColor blackColor].CGColor;
    imageView.layer.shadowOpacity = 0.1;
    imageView.layer.shadowRadius = 5;
    imageView.layer.shadowOffset = CGSizeMake(5, 5);
    [imageView setClipsToBounds:NO];

remember that you need to import the QuarzCore library

0
source

Well! You can try this.

// 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 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 shadowed
[containerLayer addSublayer:image.layer];

// add container including masked image and shadow into view
[self.view.layer addSublayer:containerLayer];
0
source

All Articles