How to drop a shadow on UIImageView which has a masked image?
I don't mean a rectangular shadow - I'd like to apply the same mask effect to shadow too.
To give the Shadow Effect to UIImageView Try below code..
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;
Well! You can try this one.
// 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];
You can custom a subclass called NEWImageVIew inherit UIImageView.In NEWImageVIew you can make a property called realImageContainer,which you can set image to this property.
#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
When you want to make the image masked and shadowed, you can set image in realImageContainer and set mask to realImageContainer layer,so you get the masked layer.
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
What about shadow?Because the realImageContainer is added to NEWImageView's view,the NEWImageView's layer not used yet,you can set shadow here.
newImgView.layer.shadowColor = [UIColor blackColor].CGColor;
newImgView.layer.shadowOpacity = 0.33;
newImgView.layer.shadowRadius = 8;
newImgView.layer.shadowOffset = CGSizeMake(0, 19);
so you get a masked and shadowed image.(I use a triangle image as mask,so I get this image)
you have to set an image with a transparent background and then you add the shadow like that:
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 you have to import QuarzCore library
Related
I have a UIImageView in a UIView.
media.mediaImageView = [UIImageView new];
media.mediaImageURL = [NSURL URLWithString:url];
media.mediaImageView.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 120);
media.mediaImageView.contentMode = UIViewContentModeScaleAspectFill;
media.mediaImageView.clipsToBounds = YES;
[self.tableView addSubview:media.mediaImageView];
I would like to add a layer (or something else, a view ?) to that UIView, this layer would be filled with a plain color with alpha transparency. In the following example it would be redColor and transparency 0.5 :
But later in code the UIView is resized :
rect = CGRectMake(0, 0, newWidth, newHeight);
myUIView.frame = rect;
The color layer should automatically cover the entire UIView (whatever its size) without explicitly redrawing the layer. Is this possible ?
I can make it work but only by redrawing the UIView AND the layer.
Easy enough. Add a subview to your image view. Set this view's background to the desired color. Set it's frame to match the image view's bounds. And be sure to set this view's autoresizingMask to have both flexible width and height. That will ensure it resizes automatically when the image view's size changes.
media.mediaImageView = [UIImageView new];
media.mediaImageURL = [NSURL URLWithString:url];
media.mediaImageView.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 120);
media.mediaImageView.contentMode = UIViewContentModeScaleAspectFill;
media.mediaImageView.clipsToBounds = YES;
// create and add overlay
UIView *overlay = [[UIView alloc] initWithFrame:media.mediaImageView.bounds];
overlay.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.5]; // as needed
overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[media.mediaImageView addSubview:overlay];
i'm trying to make a UIImageView with rounder corner and white border, i have subclassed a UIImageView, this is the code:
MyUIImageView.h
#interface MyUIImageView : UIImageView
#end
MyUIImageView.m
#implementation MyUIImageView
-(void)layoutSubviews
{
self.layer.cornerRadius = CGRectGetWidth(self.frame)/2.f;
self.layer.borderColor = [[UIColor whiteColor] CGColor];
self.layer.borderWidth = kLineWidth;
self.layer.masksToBounds = YES;
self.clipsToBounds = YES;
self.contentMode = UIViewContentModeScaleAspectFill;
self.backgroundColor = [UIColor colorWithRed:0.82 green:0.82 blue:0.83 alpha:1];
}
#end
this is the result:
seems fine, but there is a problem as you can see from here:
the image pops out from the borders edge, how i can avoid this problem? how i can cut the image exactly at the edge of the border?
Perhaps a better solution doesn't involve making another View at all - with two views you greatly increase complexity for animation etc, not to mention overhead to keep track of and manipulate both.
I'd instead create a shape layer and add it as a sublayer. Something like this:
CAShapeLayer border = [[CAShapeLayer alloc] init];
border.path = [UIBezierPath bezierPathWithRoundedRect: self.bounds cornerRadius: self.layer.cornerRadius];
border.strokeColor = [UIColor whiteColor];
border.fillColor = [UIColor clearColor];
self.layer.addSublayer(border)
The benefit of doing it this way is that you can add it as a method on your UIImageView subclass if you wish. You can add a border to an object and forget about it, as long as you're not changing the frame of the base object. Transforms etc affect sublayers so you can scale, rotate, etc and not have gross edges.
Hope this helps!
Create a custom border like this:
UIImage *image = [UIImage imageNamed:#"spongebob.jpg"];
UIView *borderView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 200, 200)];
[borderView.layer setMasksToBounds:YES];
[borderView.layer setCornerRadius:borderView.frame.size.width/2.0f];
[borderView setBackgroundColor:[UIColor whiteColor]];
int borderWidth = 3.0f;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(borderWidth, borderWidth, borderView.frame.size.width-borderWidth*2, borderView.frame.size.height-borderWidth*2)];
[imageView.layer setCornerRadius:imageView.frame.size.width/2.0f];
[imageView.layer setMasksToBounds:YES];
[imageView setImage:image];
[borderView addSubview:imageView];
[self.view addSubview:borderView];
Now you image does not pop out of the border.
Hope this helps :)
I have a UIViewController with a UIImageView (imageView), and I'm defining several layers that will be nested in the imageview as follows in viewDidLoad:
//container layer - the very top layer
CALayer *containerLayer = [CALayer layer];
containerLayer.opacity = 0;
containerLayer.bounds = [self.imageView.layer frame];
// Holder Layer
CALayer *holderLayer = [CALayer layer];
holderLayer.opacity = 0;
holderLayer.bounds = self.imageView.bounds;
// Hierarchy layers
[containerLayer setValue:holderLayer forKey:#"__holderLayer"];
[containerLayer addSublayer:holderLayer];
[self.imageView.layer addSublayer:containerLayer];
I have the following code that works when loading an image from a UIImagePicker :
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageView *newImgView = [[UIImageView alloc] initWithFrame:self.imageView.frame];
CGRect frame = self.imageView.frame;
newImgView.image = image;
frame.origin = CGPointMake(0, 0);
newImgView.layer.frame = frame;
newImgView.layer.opacity = .9;
newImgView.layer.contentsGravity = kCAGravityResizeAspectFill;
CALayer * containerLayer = self.imageView.layer.sublayers[0];
if (containerLayer != nil)
{
[containerLayer setValue:newImgView.layer forKey:#"__imageLayer"];
CALayer * holderLayer = [containerLayer valueForKey:#"__holderLayer"];
if (holderLayer != nil)
{
//!!!!!line below doesn't work!!!
//[holderLayer addSublayer:newImgView.layer];
//line below works!
[self.imageView.layer addSublayer:newImgView.layer];
}
}
[self.imageView setNeedsDisplay];
[self checkAndPrintLayers];
so the first nested layer is containerLayer, then holderLayer, and I'm expecting to add various images as sublayers to the holderLayer and then manipulate it. However, calling
[holderLayer addSublayer:newImgView.layer];
doesn't work; the imageView stays blank. However, calling
[self.imageView.layer addSublayer:newImgView.layer];
and adding a sublayer to the top layer works just dandy.
Am I missing something obvious here? Would love any suggestions.
thanks.
Because
holderLayer and containerLayer are transparent (holderLayer.opacity = 0 / containerLayer.opacity = 0),
Make it,
//container layer - the very top layer
CALayer *containerLayer = [CALayer layer];
containerLayer.opacity = 1.0;
containerLayer.bounds = [self.imageView.layer frame];
// Holder Layer
CALayer *holderLayer = [CALayer layer];
holderLayer.opacity = 1.0;
holderLayer.bounds = self.imageView.bounds;
I can add shadow to imageView layer using the following code.
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"]];
self.imageView.center = self.view.center;
CALayer *containerLayer= [CALayer layer];
containerLayer.shadowColor = [UIColor blackColor].CGColor;
containerLayer.shadowRadius = 10.0f;
containerLayer.shadowOffset = CGSizeMake(10.0f, 5.0f);
containerLayer.shadowOpacity = .8f;
[containerLayer addSublayer:self.imageView.layer];
[self.view.layer addSublayer:containerLayer];
1 . The problem is that I don't know why I have to add imageView.layer to containerLayer to get the imageView shadow effect. However, if I add containerLayer to imageView.layer, there's no shadow in imageView, why?
The error code is:
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"]];
self.imageView.center = self.view.center;
CALayer *containerLayer= [CALayer layer];
/*same as before*/
[self.imageView.layer addSublayer:containerLayer];
[self.view.layer addSublayer:self.imageView.layer];
Question2: the containerLayer's(used to provide shadow to imageView) frame = {{0, 0}, {0, 0}}, but the final position is in the center of screen. why?
A layer needs something opaque inside it to create a shadow around (unless you've specified a shadowPath explicitly). So your first version of the code works because the containerLayer has the imageView's layer as a sublayer. But, as you noticed from Question #2, the containerLayer's frame indicates that it is actually located in the upper left corner with a size of (0,0). The reason you can still see the image is that containerLayer is not masking to its bounds. Add this line to your first version, and the image disappears:
[containerLayer setMasksToBounds: YES]; // kitten (and shadow) is gone
Your version #2 of the code does not display a shadow because, in part, the containerLayer does not "contain" anything. If you use version #2 but give the containerLayer a new frame and an opaque background color, a shadow appears. (But this obviously isn't a solution, because the image is covered up...) Also note that there is no shadow when the layer's background is [UIColor clearColor].
[self.imageView.layer addSublayer:containerLayer];
containerLayer.frame = self.imageView.layer.bounds;
containerLayer.backgroundColor = [UIColor yellowColor].CGColor; // yellow box w/shadow
// containerLayer.backgroundColor = [UIColor clearColor].CGColor; // no shadow here
If you wanted to have a container with a shadow that houses the UIImageView, you could do something like this:
UIView * shadowView = [[UIView alloc] initWithFrame: self.imageView.frame];
shadowView.layer.shadowColor = [UIColor blackColor].CGColor;
shadowView.layer.shadowRadius = 10.0f;
shadowView.layer.shadowOffset = CGSizeMake(10.0f, 5.0f);
shadowView.layer.shadowOpacity = .8f;
[self.view addSubview: shadowView];
self.imageView.frame = (CGRect) { CGPointZero, self.imageView.frame.size };
[shadowView addSubview: self.imageView];
A CALayer could be used instead of a UIView in a similar way. Or you could apply shadow attributes to the imageView's layer directly, making sure that neither the view nor the layer is clipping/masking to bounds.
If you want to add a shadow over the imageview just change the alpha value (0.0 min - 1.0 max) of that imageview. This will give you the shadow effect and whenever you want to remove the shadow just restore the alpha value to 1.0.
For ex:
self.imageview.alpha = 0.5 // shadow state
self.imageview.alpha = 1.0 // original state
I want to add a white border to an UIImageView. I tried the following, but it didn't work:
UIImage *image = [UIImage imageNamed:imagePath];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.width = rect.size.width + 1; // create white right border
[imageView setBackgroundColor:[UIColor whiteColor]];
imageView.frame = rect;
[myViewView addSubview:imageView];
I now did it like
CALayer *sublayer = [CALayer layer];
sublayer.backgroundColor = [UIColor whiteColor].CGColor;
sublayer.frame = CGRectMake(imageView.bounds.size.width, 0, 1, imageView.frame.size.height);
[imageView.layer addSublayer:sublayer];
You can create a UIView that is larger than the UIImage view, and set the image view's frame to be 0,0, width+1, height, and that will add the extra to the side.