How to round corners and add border to image (UIImage)

On a recent project I had to take an otherwise square image and add both rounded corners and a border. Here is an image similar to what I started with:

UIImage of Lake

And here’s the code to create the image and imageview, add a border and round the corners:

// Define a new image object
UIImage *image = [UIImage imageNamed:@"lake.png"];
// Define a new image view
UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 157, 125)];
// Set image in imageview and add to view
[imgView setImage:image];
[[self view] addSubview:imgView];
// Create a white border with defined width
imgView.layer.borderColor = [UIColor whiteColor].CGColor;
imgView.layer.borderWidth = 1.5;
// Set image corner radius
imgView.layer.cornerRadius = 5.0;
// To enable corners to be "clipped"
[imgView setClipsToBounds:YES];

The final image looks as follows:

UIImage of Lake with rounded corners and border