UIImageView带边距的圆边框

时间:2014-09-10 14:39:12

标签: ios objective-c uiimageview

我正在使用此代码创建更圆的UIImageView:

-(void) makeImageViewRounded {
    self.layer.backgroundColor=[[UIColor greenColor] CGColor];
    self.layer.cornerRadius= self.frame.size.height /2;
    self.clipsToBounds = YES;
    self.layer.masksToBounds = YES;
    self.layer.borderWidth=1.5;
    self.layer.borderColor=[[UIColor grayColor] CGColor];
}

这是结果(忽略背景,如果jpg ..它从圆角​​边界开始):

enter image description here

我想在图像和圆形边框之间创建一个边距到此结果(如果jpg,则忽略背景......它从圆角​​边框开始):

enter image description here

我如何实现这一目标?

2 个答案:

答案 0 :(得分:0)

我想办法,在其实现中创建一个名为CircleView的UIView子类,添加

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setBackgroundColor:[UIColor clearColor]];

    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 20.0);
    CGContextSetStrokeColorWithColor(context,
                                     [UIColor lightGrayColor].CGColor);
    CGRect rectangle = self.bounds;
    CGFloat lineWidth = self.superview.layer.borderWidth;
    rectangle = CGRectInset(rectangle, lineWidth, lineWidth);
    CGContextAddEllipseInRect(context, rectangle);
    CGContextStrokePath(context);
}


     //Then add this circle view to your imageView
    CircleView *circle = [[CircleView alloc]initWithFrame:imageView.bounds];
    [imageView addSubview:circle];

答案 1 :(得分:-1)

您可以使用UIImageView图层的setContentsRect:方法执行此操作。