Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to set Border of an UIImage

I am new to swift and I am trying to set a black border around my UIImageView but I am unable to do so. This is my code

@IBOutlet weak var flagImage: UIImageView!
var image = UIImage(named: "estonia")

override func viewDidLoad() {
    super.viewDidLoad()

    flagImage.layer.borderColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0).CGColor
    flagImage.layer.cornerRadius = 5.0

    flagImage.contentMode = .scaleAspectFit
    flagImage.image = image
 }

Could I get some help on this?

like image 824
manu28 Avatar asked Jul 10 '15 11:07

manu28


People also ask

How do I add a border in SwiftUI?

To change that, and make the shape view act as a border, all we have to do is to apply a stroke to it, passing as argument the desired color. Text("Borders in SwiftUI!") Text("Borders in SwiftUI!")

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

How do you declare UIImage in Objective C?

For example: UIImage *img = [[UIImage alloc] init]; [img setImage:[UIImage imageNamed:@"anyImageName"]]; My UIImage object is declared in . h file and allocated in init method, I need to set image in viewDidLoad method.


2 Answers

You gave the border color to the imageView but how could you expect to see the color with out any area to fill in. So, the

flagImage.layer.borderWidth = 2 /** as you wish **/

Gives the Area around the imageView of thickness 2 to fill in the color you gave.

like image 128
Bishow Gurung Avatar answered Sep 22 '22 04:09

Bishow Gurung


Try to add this

Objective-C

flagImage.layer.masksToBounds = YES;

Swift

flagImage.layer.masksToBounds = true
like image 45
Jaeger Avatar answered Sep 24 '22 04:09

Jaeger