CAShapeLayer resulting in no image in UIImageView - ios

I'm trying to make uiimageview top corners rounded but when CASharpeLayer is drawn image gets disappear, i don't know what causing this issue i've put below code in willDisplayCell. when cell is reused images appears properly with drawn layer
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.imgProduct.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(3.0,3.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = cell.imgProduct.bounds;
maskLayer.path = maskPath.CGPath;
cell.imgProduct.layer.mask = maskLayer;
cell.imgProduct.layer.masksToBounds=NO;

I've solved this by myself...:P i've drawn the mask in drawRect method of my UICollectionViewCell subclass
- (void)drawRect:(CGRect)rect {
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.imgProduct.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(3.0, 3.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.imgProduct.bounds;
maskLayer.path = maskPath.CGPath;
_imgProduct.layer.mask = maskLayer;
_imgProduct.layer.masksToBounds=YES;
}

Related

Rounded Corner of two Label in ios

I have two label left and right mentioned .
Now i am trying to rounded the corner from left of left label and right of right label
I have tried this:
cell.rightLabel.layer.cornerRadius = UIRectCornerBottomLeft | UIRectCornerTopLeft;
(this code is also not working all border are rounded ..)
cell.rightLabel.layer.cornerRadius = 8;
cell.leftlabel.layer.cornerRadius = UIRectCornerBottomRight | UIRectCornerTopRight;
cell.leftlabel.layer.cornerRadius = 8;
Set the property clipsToBounds to YES or use MasksToBounds to YES
cell.rightLabel.clipsToBounds = YES;
cell.leftlabel.clipsToBounds = YES;
try this
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.rightLabel.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft ) cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = cell.rightLabel.bounds;
maskLayer.path = maskPath.CGPath;
cell.rightLabel.layer.mask = maskLayer;
Try this :
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.label.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft ) cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path = maskPath.CGPath;
self.label.layer.mask = maskLayer;
i hope its work.....

Unable to apply UIBezierPath in CellForRowAtIndexPath before scroling the TableView

Using This code I got My Profile Picture in Proper D shape but after When i scrolling the tableView not when we are entering in to view controller.
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:cellimg.boundsbyRoundingCorners:(UIRectCornerBottomRight|UIRectCornerTopRight)cornerRadii:CGSizeMake(40.0, 40.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame =cellimg.bounds;
maskLayer.path = maskPath.CGPath;
cellimg.layer.mask = maskLayer;
Use D shape mask image with below code it may be work for you
CALayer *mask = [CALayer layer];
mask.contents = (id)[UIImage imageNamed:#“your_D_ShapeMask.png"].CGImage;
mask.frame = CGRectMake(0, 0, 100, 100);
[cellimg.layer setMask:mask];

why this code not perfect work on iphone6

I want to exact bottom round shape on UIImageView. Here is my code that perfectly work on iPhone5 but when I run this code on iPhone6 that not work perfect..
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.imageView.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(150.0, 150.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.imageView.bounds;
maskLayer.path = maskPath.CGPath;
self.imageView.layer.mask = maskLayer;

Adding Rounded Corners to only top of UITableView?

I am trying to add rounded corners to only the top corners of my UITableView but the problem is, with the code below, it just makes a black layer over my whole UITableView. How would I fix this?
//Rounded Corners for top corners of UITableView
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:thetableView.bounds
byRoundingCorners:UIRectCornerTopLeft
cornerRadii:CGSizeMake(10.0, 10.0)];
UIBezierPath *maskPath2 = [UIBezierPath bezierPathWithRoundedRect:thetableView.bounds
byRoundingCorners:UIRectCornerTopRight
cornerRadii:CGSizeMake(10.0, 10.0)];
// Create the shape layer and set its path
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = thetableView.bounds;
maskLayer.path = maskPath.CGPath;
CAShapeLayer *maskLayer2 = [CAShapeLayer layer];
maskLayer.frame = thetableView.bounds;
maskLayer.path = maskPath2.CGPath;
// Set the newly created shape layer as the mask for the image view's layer
[thetableView.layer addSublayer:maskLayer];
[thetableView.layer addSublayer:maskLayer2];
the following code works for me for a navigationbar, just change the first line and put your self.tableView:
CALayer *capa = [self.navigationController navigationBar].layer;
[capa setShadowColor: [[UIColor blackColor] CGColor]];
[capa setShadowOpacity:0.85f];
[capa setShadowOffset: CGSizeMake(0.0f, 1.5f)];
[capa setShadowRadius:2.0f];
[capa setShouldRasterize:YES];
//Round
CGRect bounds = capa.bounds;
bounds.size.height += 10.0f; //I'm reserving enough room for the shadow
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;
[capa addSublayer:maskLayer];
capa.mask = maskLayer;
I think the best way to make a background view for the table view which view has rounded top corner.Its just a solution to solve the problem.May be it will help you.

image corner radius only bottom left and right

I want to corner radius exact in round shape only bottom corner left and right. I already done it but bottom shape not look like exact round so how can I solve this problem
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.imageView.bounds byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.imageView.bounds;
maskLayer.path = maskPath.CGPath;
self.imageView.layer.mask = maskLayer;
I have the same issues and I solved it like this :
- (void)setMaskTo:(UIView*)view {
CAShapeLayer *rect = [[CAShapeLayer alloc] init];
[rect setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height/2)];
rect.backgroundColor = ([UIColor grayColor]).CGColor;
UIBezierPath *shape = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,rect.frame.size.height/2,self.view.frame.size.width,self.view.frame.size.height/2)];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = shape.CGPath;
shapeLayer.fillColor = [UIColor grayColor].CGColor;
CAShapeLayer *viewMask = [[CAShapeLayer alloc] init];
[viewMask addSublayer:shapeLayer];
[viewMask addSublayer:rect];
view.layer.mask = viewMask;
}

Resources