Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set border around UIImageView

I want to apply two types of border on a UIImageView:

  1. One is the border on the layer of the UIImageView.
  2. Second is the border around the layer of the UIImageView.

How can I do this?

like image 269
Girish Avatar asked Jun 25 '12 15:06

Girish


People also ask

How to add border to imageview image in Android?

Adding border to ImageView image makes your ImageView highlight from the activity layout surface so image will look individually on application. We can easily set border on ImageView using external layout.xml file setting as ImageView background.

How to display imageview image with border using XML file?

Add stroke on ImageView image to display image with border using xml file. Adding border to ImageView image makes your ImageView highlight from the activity layout surface so image will look individually on application. We can easily set border on ImageView using external layout.xml file setting as ImageView background.

How do I add a border to a UIView?

Adding a border to a UIView can be implemented by setting the borderColor and borderWidth properties on the view’s layer: An @IBDesignable UIView extension marking the border properties as @IBInspectable can be implemented to allow view borders to be configured in a Storyboard:

How do I draw a shadow in a UIImageView?

Shadows are drawn outside of a view’s bounds, meaning clipToBounds and the view’s layer masksToBounds properties must be false for the shadow to be visible. A UIImageView with contentMode set to .aspectFill, .center, and others will draw the image outside of the UIImageView bounds if clipToBounds is false.


3 Answers

Try

#define kBorderWidth 3.0
#define kCornerRadius 8.0
CALayer *borderLayer = [CALayer layer];
CGRect borderFrame = CGRectMake(0, 0, (imageView.frame.size.width), (imageView.frame.size.height));
[borderLayer setBackgroundColor:[[UIColor clearColor] CGColor]];
[borderLayer setFrame:borderFrame];
[borderLayer setCornerRadius:kCornerRadius];
[borderLayer setBorderWidth:kBorderWidth];
[borderLayer setBorderColor:[[UIColor redColor] CGColor]];
[imageView.layer addSublayer:borderLayer];

And don't forget to import QuartzCore/QuartzCore.h

This example will draw a boarder on the layer, but change it's frame slightly to make the border around the layer.

like image 193
Jonathan King Avatar answered Oct 11 '22 11:10

Jonathan King


Another way

You must import

#import <QuartzCore/QuartzCore.h>

Then add code for your UIImageView

imgView.clipsToBounds = YES;
imgView.layer.cornerRadius = 8.0;
imgView.layer.borderWidth = 2.0;
imgView.layer.borderColor = [UIColor greenColor].CGColor;
like image 43
MaxEcho Avatar answered Oct 11 '22 11:10

MaxEcho


An other way is to add another layer that goes a bit outside of the UIImageView's layer like so :

CALayer * externalBorder = [CALayer layer];
externalBorder.frame = CGRectMake(-1, -1, myView.frame.size.width+2, myView.frame.size.height+2);
externalBorder.borderColor = [UIColor blackColor].CGColor;
externalBorder.borderWidth = 1.0;

[myView.layer addSublayer:externalBorder];
myView.layer.masksToBounds = NO;
like image 23
Hugues Duvillier Avatar answered Oct 11 '22 10:10

Hugues Duvillier