2014-09-10 101 views
0

的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

我该如何做到这一点?

+0

添加保证金 – rptwsthi 2014-09-10 14:40:36

+0

@rptwsthi另一个图层蒙版,可以请你提供一些额外的代码? – 2014-09-10 14:45:06

回答

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];