我使用的是 Round Image View 类……所以我只是使用它而不是 UIImageView,并且没有什么可调整的……</p>
此类还在圆周围绘制可选边框。圆形图片周围通常有边框。
它不是 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); // Half border is clipped
[_strokeColor setStroke];
CGContextAddPath(ctx, path);
CGContextStrokePath(ctx);
}
CGPathRelease(path);
}
@end