UIImage border customization

Is there any way to set the bounds for UIImage. I know how to set it for a UIImageView, but my problem is that the UIImage that I load into the UIImageview will not be the same size or proportion as the UIImageView. Consequently, Ive retained the UIImageView mode to fit the aspect. Providing a UIImageView border would now limit the entire UIImageView, not just the UIImage, and this does not look very good when the UIImage does not have the same size or format as the UIV.

Help?

+5
source share
3 answers
+3
#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
+1
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;
}
0
source

All Articles