I have a UIImageView
on each of my UITableView
cells, that display a remote image (using SDWebImage
). I\'ve done some QuartzCore
I use a Round Image View class… So I just use it instead of UIImageView, and have nothing to tweak…
This class also draws an optional border around the circle. There is often a border around rounded pictures.
It is not a UIImageView subclass because UIImageView has its own rendering mechanism, and does not call the drawRect method..
Interface :
#import
@interface MFRoundImageView : UIView
@property(nonatomic,strong) UIImage* image;
@property(nonatomic,strong) UIColor* strokeColor;
@property(nonatomic,assign) CGFloat strokeWidth;
@end
Implementation :
#import "MFRoundImageView.h"
@implementation MFRoundImageView
-(void)setImage:(UIImage *)image
{
_image = image;
[self setNeedsDisplay];
}
-(void)setStrokeColor:(UIColor *)strokeColor
{
_strokeColor = strokeColor;
[self setNeedsDisplay];
}
-(void)setStrokeWidth:(CGFloat)strokeWidth
{
_strokeWidth = strokeWidth;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGPathRef path = CGPathCreateWithEllipseInRect(self.bounds, NULL);
CGContextAddPath(ctx, path);
CGContextClip(ctx);
[self.image drawInRect:rect];
if ( ( _strokeWidth > 0.0f ) && _strokeColor ) {
CGContextSetLineWidth(ctx, _strokeWidth*2); // Half border is clipped
[_strokeColor setStroke];
CGContextAddPath(ctx, path);
CGContextStrokePath(ctx);
}
CGPathRelease(path);
}
@end