-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIImage+WDExtra.m
More file actions
53 lines (45 loc) · 1.87 KB
/
UIImage+WDExtra.m
File metadata and controls
53 lines (45 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// UIImage+Extra.m
// iOS-Project
//
// Created by wudan on 2018/12/24.
// Copyright © 2018 wudan. All rights reserved.
//
#import "UIImage+WDExtra.h"
@implementation UIImage (WDExtra)
#pragma mark - 设置图片读取模式
+ (UIImage *)wd_initWithName:(NSString *)name renderingMode:(UIImageRenderingMode)mode {
UIImage *image = [[UIImage imageNamed:name] imageWithRenderingMode:mode];
return image;
}
#pragma mark - 用颜色返回一张图片
+ (UIImage *)wd_imageWithColor:(UIColor *)color {
return [self wd_imageWithColor:color size:CGSizeMake(1, 1)];
}
#pragma mark - 用颜色返回一张图片
+ (UIImage *)wd_imageWithColor:(UIColor *)color size:(CGSize)size {
if (!color || size.width <= 0 || size.height <= 0) return nil;
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
#pragma mark - 高效添加圆角图片
- (UIImage *)wd_imageAddCornerWithRadius:(CGFloat)radius andSize:(CGSize)size {
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)];
CGContextAddPath(ctx,path.CGPath);
CGContextClip(ctx);
[self drawInRect:rect];
CGContextDrawPath(ctx, kCGPathFillStroke);
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
@end