Setting a UIImage border

12,949

Solution 1

There's some solutions in this question: How can i take an UIImage and give it a black border?

Solution 2

#import <QuartzCore/CALayer.h>


UIImageView *imageView = [UIImageView alloc]init];
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor blackColor].CGColor;
imageView.layer.borderWidth = 1;
imageView.layer.cornerRadius = 10; //optional
Share:
12,949
Admin
Author by

Admin

Updated on June 22, 2022

Comments

  • Admin
    Admin almost 2 years

    Is there a way to set borders to a UIImage. I know how to set it for UIImageView, but my problem is the UIImage that I load up in a UIImageview wont be of the same size or aspect ratio as the UIImageView. Hence Ive kept the UIImageView mode to aspect fit. Giving a border to UIImageView now would border the entire UIImageView rather than just the UIImage, and that doesn't look good when the UIImage is not of the same size or aspect ratio as the UIV.

    Help?

  • Matt Connolly
    Matt Connolly over 12 years
    Two problems here. That other question is about rendering a UIImage directly, not using a UIImageView. Also, the best solution places the border around the frame of the UIImageView, not the aspect fitted image within the UIImageView.
  • Matt Connolly
    Matt Connolly over 12 years
    Actually, the other question is similar, it's just the popular answer there that doesn't help here.
  • alexmorhun
    alexmorhun about 10 years
    How can I set border width?