我使用的是Round Image View类……所以我只用它而不是UIImageView,也没有什么要调整的……
该类还在圆周围绘制可选边框。圆形图片周围经常有边框。
它不是UIImageView子类,因为UIImageView具有其自己的呈现机制,并且不调用drawRect方法。
介面
#import <UIKit/UIKit.h>
@interface MFRoundImageView : UIView
@property(nonatomic,strong) UIImage* image;
@property(nonatomic,strong) UIColor* strokeColor;
@property(nonatomic,assign) CGFloat strokeWidth;
@end
实施方式:
#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);
[_strokeColor setStroke];
CGContextAddPath(ctx, path);
CGContextStrokePath(ctx);
}
CGPathRelease(path);
}
@end