Adding border to image in cell's imageView - ios

I'd like to add a border to a TVC cell's image through the imageView and the imageView layer
I've got code that works for the cell.layer itself, but as soon as I change it to the cell.imageView.layer, the code does nothing at all.
I'm using it within tableView:cellForRowAtIndexPath and am in Xcode 4.6.3, iOS 6.x
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.imageView.image = [UIImage imageNamed:#"myImage.png"];
CALayer* layer;
//layer = cell.layer;
layer = cell.imageView.layer;
[layer setMasksToBounds:YES];
[layer setCornerRadius:10.0f];
[layer setBorderWidth:10.0f];
This works if applied to cell.layer, but does nothing at all to the cell's imageView.layer.
Any ideas?
Thanks.
EDIT: FYI, I am importing <QuartzCore/QuartzCore.h>.
EDIT: though this works against the cell just fine, that is because the cell already has a border color defined as black. The imageView does not and therefore requires a border color to be set like so:
[cell.imageView.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[cell.imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[cell.imageView.layer setBorderColor: [[UIColor colorWithRed:.5f green:.6f blue:.3f alpha:.75f] CGColor ]];

Another option is, add
#import <QuartzCore/QuartzCore.h>
FrameWork and use
cell.imageView.layer.borderWidth = 1.0f;
cell.imageView.layer.setMasksToBounds = YES;

Though this code works against the cell just fine, that is because the cell already has a border color defined as black. The imageView does not and therefore requires a border color to be set like so:
[cell.imageView.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[cell.imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[cell.imageView.layer setBorderColor: [[UIColor colorWithRed:.5f green:.6f blue:.3f alpha:.75f] CGColor ]];

Related

Place CALayer at very bottom of layer stack of UIButton

This seems simple enough - perhaps you can help me find the solution?
I wrote a custom drop shadow for a UIButton. I'm attempting to place it at the lowest index of the UIButton's layer stack. Unfortunately, it isn't being placed beneath the rest of the UIButton.
The Code:
//UIButton type custom
alarmSetterButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img = [UIImage imageNamed:#"rootSchedulerButton"];
//Scaling
float delta = 2.4f;
if (IS_IPHONE_5) delta = 2.0f;
[alarmSetterButton setFrame:CGRectMake(0.0f, 0.0f, (img.size.width/delta), (img.size.height/delta))];
[alarmSetterButton setBackgroundImage:img forState:UIControlStateNormal];
[alarmSetterButton setTitle:#"" forState:UIControlStateNormal];
[alarmSetterButton addTarget:self action:#selector(setSecondaryView:) forControlEvents:UIControlEventTouchUpInside];
//the colors for the gradient
UIColor *high = [UIColor colorWithWhite:0.0f alpha:1.0f];
UIColor *low = [UIColor colorWithWhite:0.0f alpha:0.0f];
//The gradient, simply enough. It is a rectangle
CAGradientLayer * gradient = [CAGradientLayer layer];
[gradient setFrame:[b bounds]];
[gradient setColors:[NSArray arrayWithObjects:(id)[high CGColor], (id)[low CGColor], nil]];
[gradient setAnchorPoint:CGPointMake(0.5f, 0.0f)];
[gradient setTransform:CATransform3DMakeRotation(315.0 / 180.0 * M_PI, 0.0, 0.0, 1.0)];
// What needs altered
[alarmSetterButton.layer insertSublayer:gradient below:alarmSetterButton.layer];
I found the solution. Instead of using UIButton's setBackroundImage:forState: method I wrote a new CALayer to contain the contents of the image.
Thanks for the help.
:)
Try this, it places the layer at position zero in the stack, i.e. the bottom.
[alarmSetterButton.layer insertSublayer:gradient atIndex:0];

Adding border around an UIImage

I am using below code to add text and border to selected image from cameraroll.
-(UIImage*)image:(UIImage*)image withText:(NSString*)text atPoint:(CGPoint)point{
if (text) {
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[image drawInRect:rect];
NSDictionary *attributes = #{NSFontAttributeName : [UIFont boldSystemFontOfSize:80],
//NSStrokeWidthAttributeName : #(4),
NSStrokeColorAttributeName : [UIColor whiteColor]};
[text drawAtPoint:point withAttributes:attributes];
CALayer *l = [self.imageView layer];
[l setBorderWidth:10.0];
[l setBorderColor:[[UIColor blueColor] CGColor]];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return newImage;
}
When i click on save button in my app, i am saving edited image to cameraroll.But edited image inside cameraroll only has text without any added border. Whether i am missing anything in above code to add and save border around image? Is there any other way to add border other than CALayer?
Replace this:
CALayer *l = [self.imageView layer];
[l setBorderWidth:10.0];
[l setBorderColor:[[UIColor blueColor] CGColor]];
With this:
UIBezierPath *rectangleBezier = [UIBezierPath bezierPathWithRect:rect];
rectangleBezier.lineWidth = 20.0f; // 10 * 2 because line drawn from the center of edge
[[UIColor blueColor] setStroke];
[rectangleBezier stroke];
Remarks:
CALayer *l = [self.imageView layer]; has nothing to do with UIImage data. imageView's layer change imageView presentation. Not the content of UIImage.
I will respond to "Adding a border around an UIImage in a UIView"
From far, the easiest method is to insert another view in the view hierarchy.
[self.view insertSubview:({
UIView * border = [[UIView alloc]
initWithFrame:CGRectInset(self.imageView.frame, -1, -1)];
border.backgroundColor = [UIColor blueColor];
border;
})
belowSubview:self.imageView];

Corner Radius leaving circular line

After setting border width and corner radius of UIView getting black circular line
Code:
afterSelectFrameView.layer.borderWidth=[slide value];
afterSelectFrameView.layer.cornerRadius=[slide value];
Apply mask to bounds
[imageView.layer setMasksToBounds:YES];
[imageView.layer setCornerRadius:5.0];
setting the border is not necessary, but here is an option for you.
[imageView.layer setBorderWidth:0.0f];
I use the code below (it is part of a ViewUtilities library) to round the corners on any view.
You could modify it to add parameters for other radii, colors, etc.
+(void)AdjustViewEdges:(UIView*)view
{
[view.layer setBorderColor: [[UIColor blackColor] CGColor]];
[view.layer setBorderWidth: 1.0];
[view.layer setCornerRadius:12.0f];
[view.layer setMasksToBounds:YES];
view.backgroundColor = [UIColor colorWithRed:.99 green:0.99 blue:0.99 alpha:1.0];
}
// You will need this as well
#import <QuartzCore/QuartzCore.h>
I don't believe it is giving me the lines you are seeing...but my background colors were different so they may be hidden...
Was this helpful?
Use it like this
afterSelectFrameView.layer.borderWidth=0.0;
afterSelectFrameView.layer.cornerRadius=[slide value];
Happy coding..........

How to remove sharp rectangle edges from UIButton control?

Take a look at the following image:
As you can see the edges are sharp rectangle and they are coming out of the rounded corner button. How can I not show the edges and only show the round button?
UPDATE (Solution):
I used the following code to achieve the round corners:
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.stopButton.bounds
byRoundingCorners:UIRectCornerAllCorners
cornerRadii:CGSizeMake(12.0, 12.0)];
// Create the shape layer and set its path
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.stopButton.bounds;
maskLayer.path = maskPath.CGPath;
// Set the newly created shape layer as the mask for the image view's layer
self.stopButton.layer.mask = maskLayer;
NEW PROBLEM:
This code gives me the error on the startButtonRound.layer.cornerRadius line.
UIButton *roundStartButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
roundStartButton.frame = CGRectMake(10, 10, 100, 20);
[roundStartButton setBackgroundImage:[UIImage imageNamed:#"green_gradient.jpg"] forState:UIControlStateNormal];
roundStartButton.layer.cornerRadius = 12.0;
UIBarButtonItem *startButton = [[UIBarButtonItem alloc] initWithCustomView:roundStartButton];
You could do this with CALayer alone:
CALayer *myLayer = [CALayer layer];
[myLayer setMasksToBounds:YES];
[myLayer setCornerRadius:5.0f];
[myLayer setBorderWidth:1.0f];
[myLayer setBorderColor: [[UIColor blackColor] CGColor]];
[myLayer setBackgroundColor: [[UIColor whiteColor] CGColor]];
You'll also want to include the QuartzCore framework and include the frameworks header in your own.
#include <QuartzCore/QuartzCore.h>
Hope this helps
Tim
I think all that can be accomplished with:
self.stopButton.layer.cornerRadius = 12.0;
But did you try
self.stopButton.opaque = NO;
self.stopButton.backgroundColor = [UIColor clearColor];
?
form the look of the image you have set your UIButton bckground color to 'White'. Make it 'Clear Color'.

How do I create a white border and black shadow for my UIImageView?

How do I create a white border and black shadow for my UIImageView?
Just import
#import <QuartzCore/QuartzCore.h>
and make sure that you have the QuartzCore framework added to your project.
Then to add border
[imageView.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];
To create shadow, see this SO question, which will get you going..
this is the way I add a border and shadow to an UIImage in a UIMageView
someImageView.image = someUIImage;
someImageView.frame = CGRectMake(45, 25, 50, 50);
[someImageView.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[someImageView.layer setBorderWidth: 2.0];
[someImageView.layer setShadowOffset:CGSizeMake(-3.0, 3.0)];
[someImageView.layer setShadowRadius:3.0];
[someImageView.layer setShadowOpacity:1.0];
remember:
#import <QuartzCore/QuartzCore.h>
You can just add it as another UIImageView behind the one showing your image.

Resources