//AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
复制代码
//AppDelegate.m
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
@end
复制代码
//ViewController.h
@interface ViewController : UIViewController
@end
复制代码
//ViewController.m
@interface ViewController ()
//@property (nonatomic,strong) UIImage *image;
@property (nonatomic,strong) UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect screen = [[UIScreen mainScreen]bounds];
CGFloat oldImageViewWidth = 50;
CGFloat oldImageViewHeigth = 50;
CGFloat oldViewTopSpacing = 200;
UIImageView *oldImageView = [[UIImageView alloc]initWithFrame:CGRectMake((screen.size.width - oldImageViewWidth)/2, oldViewTopSpacing, oldImageViewWidth, oldImageViewHeigth)];
UIImage *oimage = [UIImage imageNamed:@"ic_often_courier"];
[oldImageView setImage:oimage];
[self.view addSubview:oldImageView];
CGFloat newImageViewWidth = 50;
CGFloat newImageViewHeigth = 50;
CGFloat newViewTopSpacing = 400;
UIImageView *newImageView = [[UIImageView alloc]initWithFrame:CGRectMake((screen.size.width - newImageViewWidth)/2, newViewTopSpacing, newImageViewWidth, newImageViewHeigth)];
UIImage *nImage = [UIImage imageNamed:@"ic_often_courier"];
UIImage *newImage = [nImage imageWithTintColor:[UIColor greenColor]];
[newImageView setImage:newImage];
[self.view addSubview:newImageView];
}
@end
复制代码
//UIImage(TintColor).h
@interface UIImage(TintColor)
- (UIImage *)imageWithTintColor:(UIColor *)tintColor;
@end
复制代码
//UIImage(TintColor).m
@implementation UIImage(TintColor)
- (UIImage *)imageWithTintColor:(UIColor *)tintColor {
///UIGraphicsBeginImageContextWithOptions()创建一个临时渲染上下文,在这上面绘制原始图片。第一个参数,size,是缩放图片的尺寸,第二个参数,isOpaque 是用来决定透明通道是否被渲染。对没有透明度的图片设置这个参数为NO,可能导致图片有粉红色调。第三个参数scale是显示缩放系数。当设置成0.0,主屏幕的缩放系数将被使用,对视网膜屏显示是2.0或者更高(在iPhone6 Plus 上是 3.0)
///UIScreen对象定义了基于硬件显示的相关属性,用这个类来获取每一个设备显示屏幕的对象
///UIScreen.mainScreen.scale:计算屏幕分辨率
UIGraphicsBeginImageContextWithOptions(self.size, NO, UIScreen.mainScreen.scale);
///一个不透明类型的Quartz 2D绘画环境,相当于一个画布,你可以在上面任意绘画
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect area = {0, 0, self.size};
CGContextScaleCTM(context, 1.0, -1.0);///坐标系X,Y缩放
CGContextTranslateCTM(context, 0, -area.size.height);///坐标系平移
CGContextSaveGState(context);///压栈操作,保存一份当前图形上下文
///CGImage:是用来重绘图形,它们在应用时是按照图像的像素矩阵来绘制图片的
///CGContextClipToMask:裁剪的区域,第一个参数表示context的指针,第二个参数表示裁剪到context的区域,也就是mask图片映射到context的区域,第三个参数表示mask的图片,对于裁剪区域Rect中的点是否变化取决于mask图中的alpha值,若alpha为0,则对应裁剪Rect中的点为透明,若alpha为1,则对应裁剪Rect中的点无变化
CGContextClipToMask(context, area, self.CGImage);
[tintColor set];
CGContextFillRect(context, area);/// 填充指定的矩形
CGContextRestoreGState(context);///在没有保存之前,用这个函数还原blend mode.
CGContextSetBlendMode(context, kCGBlendModeDestinationOver);///设置blend mode.
///CGContextDrawImage允许在制定的尺寸和位置上画图,允许在特定边缘或者适应一组图片特征比如faces,裁剪图片。
CGContextDrawImage(context, area, self.CGImage);
///从当前环境当中得到重绘的图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();///关闭当前环境
///设置图片的渲染模式:始终绘制图片原始状态
return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
@end
复制代码