IOS RoundView使子ImageView被舍入

时间:2014-09-29 05:26:48

标签: ios uiview uiimageview

我创建了这个名为RoundView的自定义UIView:

#import <QuartzCore/QuartzCore.h>

@implementation RoundedView

+(UIColor *)grayUIColor {
    return [UIColor colorWithRed:161.0/255.0 green:157.0/255.0 blue:164.0/255.0 alpha:1.0];
}

+(UIColor *)darkBlueUIColor {
    return [UIColor colorWithRed:86.0/255.0 green:88.0/255.0 blue:87.0/255.0 alpha:1];
}

+(UIColor *)greenUIColor {
    return [UIColor colorWithRed:51.0/255.0 green:141.0/255.0 blue:130.0/255.0 alpha:1];
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        //add pico icon as default image
        _defaultImage = [UIImage imageNamed:@"actionBar_pico_icon"];
        _isCreated = NO;
    }
    return self;
}


-(UIView*)createRoundViewWithBorder:(UIColor*)borderColor andPaddingColor:(UIColor*)paddingColor{
    _borderColor = borderColor;
    _paddingBackgroundColor = paddingColor;
    [self setViewAppearance];
    [self addImageToView];
    _isCreated = YES;
    return self;
}

/**
 Set the current view appearance
 */
-(void) setViewAppearance {
    self.layer.borderWidth = 1.5;
    self.layer.borderColor = [_borderColor CGColor];
    self.backgroundColor = _paddingBackgroundColor;
}

-(void) addImageToView {
    CGRect frame = CGRectMake(0, 0, self.frame.size.width - 5, self.frame.size.height - 5);
    _imageView = [[UIImageView alloc] initWithFrame:frame];
    //calculate center x
    float x = (self.frame.size.width - _imageView.frame.size.width) / 2;
    //calculate center y
    float y = (self.frame.size.height - _imageView.frame.size.height) / 2;
    //create new frame
    frame = CGRectMake(x, y, _imageView.frame.size.width, _imageView.frame.size.height);
    _imageView.image = _defaultImage;
    _imageView.frame = frame;
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:_imageView];
    [self makeViewRounded:_imageView];
    [self makeViewRounded:self];
}


-(UIView*) makeViewRounded:(UIView*)view {
    //set the look of the image
    view.layer.cornerRadius= self.frame.size.height /2;
    view.layer.opaque = NO;
    view.layer.masksToBounds = YES;
    return view;
}

-(void)updateImage:(UIImage *)image {
    _image = image;
    _imageView.image = image;
}

-(void)reset {
    [self updateImage:_defaultImage];
}

@end

输出的示例将是:

enter image description here

如果仔细观察,您会发现边框是一个圆圈,但图像视图有边缘。

如何使图像平滑?

2 个答案:

答案 0 :(得分:2)

self.imgViewbg.layer.cornerRadius = self.imgViewbg.frame.size.width / 2;
self.imgViewbg.clipsToBounds = YES;

你可以试试这个代码。并在您的项目中实施.. 它肯定会工作..对于你

答案 1 :(得分:0)

我认为问题似乎在这里:

view.layer.cornerRadius= self.frame.size.height /2;

尝试给它一个恒定的小数字并查看更改。可能是高度/ 2不是一个完美的圆圈。您可以给出比height / 2更小的值并查看更改。通过观察你的形象,这是一个疯狂的猜测。

相关问题