有没有办法给UIImage设置边框呢?我知道如何为UIImageView设置边框,但我的问题是,我加载到UIImageView中的UIImage可能与UIImageView不同大小或纵横比。因此,我将UIImageView模式设置为适应宽高比。现在为UIImageView添加边框会将整个UIImageView边框起来,而不仅仅是UIImage,当UIImage与UIImageView不同时,这看起来并不好。有什么帮助吗?
这个问题有一些解决方案:如何给UIImage添加黑色边框?
#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
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;
}