Rupert
Rupert

Reputation: 51

Setting a UIImage border

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?

Upvotes: 5

Views: 12978

Answers (3)

Sameera R.
Sameera R.

Reputation: 4592

#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

Upvotes: 1

user2155906
user2155906

Reputation: 149

image with border

+(UIImage*)imageWithBorderFromImage:(UIImage*)source
{

    CGSize size = [source size];

    UIGraphicsBeginImageContext(size);
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    [source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context,(255/255.f),(255/255.f),(255/255.f), 1.0);
    CGContextStrokeRect(context, rect);
    UIImage *testImg =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return testImg;
}

Upvotes: 0

Matt Williamson
Matt Williamson

Reputation: 40233

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

Upvotes: 3

Related Questions