设置UIImage边框

5
有没有办法给UIImage设置边框呢?我知道如何为UIImageView设置边框,但我的问题是,我加载到UIImageView中的UIImage可能与UIImageView不同大小或纵横比。因此,我将UIImageView模式设置为适应宽高比。现在为UIImageView添加边框会将整个UIImageView边框起来,而不仅仅是UIImage,当UIImage与UIImageView不同时,这看起来并不好。有什么帮助吗?
3个回答

3

这里有两个问题。那个问题是直接呈现UIImage,而不是使用UIImageView。此外,最佳解决方案是将边框放在UIImageView的框架周围,而不是UIImageView中适合纵横比的图像。 - Matt Connolly
实际上,另一个问题很相似,只是那里的流行答案对这里没有帮助。 - Matt Connolly

1
#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

0
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;
}

我该如何设置边框宽度? - alexmorhun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接