标签:imp 封装数据 control idt mcu pat out isp atom
封装 :生成头像(UIImage (NJ).h / .m
@interface UIImage (NJ) /** * 生成头像 * * @param icon 头像图片名称 * @param border 头像边框大小 * @param color 头像边框的颜色 * * @return 生成好的头像 */ + (instancetype)imageWithIcon:(NSString *)icon border:(NSInteger)border color:(UIColor *)color; @end
@implementation UIImage (NJ) + (instancetype)imageWithIcon:(NSString *)icon border:(NSInteger)border color:(UIColor *)color { // 0. 加载原有图片 UIImage *image = [UIImage imageNamed:icon]; // 1.创建图片上下文 CGFloat margin = border; CGSize size = CGSizeMake(image.size.width + margin, image.size.height + margin); // YES 不透明 NO 透明 UIGraphicsBeginImageContextWithOptions(size, NO, 0); // 2.绘制大圆 CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, size.width, size.height)); [color set]; CGContextFillPath(ctx); // 3.绘制小圆 CGFloat smallX = margin * 0.5; CGFloat smallY = margin * 0.5; CGFloat smallW = image.size.width; CGFloat smallH = image.size.height; CGContextAddEllipseInRect(ctx, CGRectMake(smallX, smallY, smallW, smallH)); // [[UIColor greenColor] set]; // CGContextFillPath(ctx); // 4.指点可用范围, 可用范围的适用范围是在指定之后,也就说在在指定剪切的范围之前绘制的东西不受影响 CGContextClip(ctx); // 5.绘图图片 [image drawInRect:CGRectMake(smallX, smallY, smallW, smallH)]; // 6.取出图片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); return newImage; }
调用
@property (weak, nonatomic) IBOutlet UIImageView *iv; @end @implementation NJViewController - (void)viewDidLoad { [super viewDidLoad]; UIImage *newImage = [UIImage imageWithIcon:@"me" border:50 color:[UIColor greenColor]]; self.iv.image = newImage; }
标签:imp 封装数据 control idt mcu pat out isp atom
原文地址:http://www.cnblogs.com/liuwj/p/6597420.html