将边框添加到圆角UIImage

时间:2014-05-22 14:58:39

标签: ios objective-c uiimage

我有一个矩形的UIImage,我希望它是圆形的并带有边框,我在SO上发现了一个问题,使它圆润并起作用,这就是代码:

- (UIImage*) roundCorneredImage: (UIImage*) orig radius:(CGFloat) r {
    UIGraphicsBeginImageContextWithOptions(orig.size, NO, 0);
    [[UIBezierPath bezierPathWithRoundedRect:(CGRect){CGPointZero, orig.size}
                                cornerRadius:r] addClip];
    [orig drawInRect:(CGRect){CGPointZero, orig.size}];
    UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;
}

但是我知道我没有尝试在图像周围做出白色边框我怎么做?

2 个答案:

答案 0 :(得分:4)

试试这个:

- (UIImage*) roundCorneredImage: (UIImage*) orig radius:(CGFloat) r {
    UIGraphicsBeginImageContextWithOptions(orig.size, NO, 0);
    UIBezierPath *bezierPath =[UIBezierPath bezierPathWithRoundedRect:(CGRect){CGPointZero, orig.size}
                                                         cornerRadius:r];
    [bezierPath setLineWidth:6.0];
    [[UIColor whiteColor] setStroke];
    [bezierPath stroke];
    [bezierPath addClip];

    [orig drawInRect:(CGRect){CGPointZero, orig.size}];
    UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return result;
}

答案 1 :(得分:0)

您可以使用UIImageView以下列方式显示它:

UIImageView *anImageView = [[UIImageView alloc] initWithImage:[UIImage imageName:@"your_image"]]; 
anImageView.layer.masksToBounds = YES;
anImageView.layer.borderWidth = 0.5f;
anImageView.layer.borderColor = [UIColor darkGrayColor].CGColor;
anImageView.layer.cornerRadius = 8.0f;