设置UIImage边框

时间:2010-09-26 04:35:49

标签: iphone objective-c cocoa-touch xcode uiimage

有没有办法为UIImage设置边框。我知道如何为UIImageView设置它,但我的问题是我在UIImageview中加载的UIImage与UIImageView的大小和宽高比不同。因此,我保持UIImageView模式适应方面。现在为UIImageView提供边框将覆盖整个UIImageView而不仅仅是UIImage,当UIImage与UIV的大小或宽高比不同时,这看起来不太好。

帮助?

3 个答案:

答案 0 :(得分:3)

这个问题有一些解决方案:How can i take an UIImage and give it a black border?

答案 1 :(得分: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

答案 2 :(得分: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;
}