当前位置: 首页 > 科技观察

iOS—改变矢量图的颜色

时间:2023-03-13 12:15:40 科技观察

为什么要改变矢量图的颜色?就请设计师剪个图吧。主要是最近设计师回家了,没办法只有一张图,懒得用PS了。我让自己成为了一个主题。其实也很简单。在ps中,不只是选中选区,填充颜色,然后保存。在iOS中也是类似的。//UIImage+ImageColor.h#import@interfaceUIImage(ImageColor)/***修改矢量颜色**@parammaskColor修改颜色*@return*/-(UIImage*)imageMaskWithColor:(UIColor*)maskColor;@end//UIImage+ImageColor.m#import"UIImage+ImageColor.h"@implementationUIImage(ImageColor)-(UIImage*)imageMaskWithColor:(UIColor*)maskColor{if(!maskColor){returnnil;}UIImage*newImage=nil;CGRectimageRect=(CGRect){CGPointZero,self.size};UIGraphicsBeginImageContextWithOptions(imageRect.size,NO,self.scale);CGContextRefcontext=UIGraphicsGetCurrentContext();CGContextScaleCTM(上下文,1.0,-1.0);CGContextTranslateCTM(上下文,0.0,-(imageRect.size.height));CGContextClipToMask(context,imageRect,self.CGImage);//选择选区得到不透明区域路径CGContextSetFillColorWithColor(context,maskColor.CGColor);//设置颜色CGContextFillRect(context,imageRect);//绘制newImage=UIGraphicsGetImageFromCurrentImageContext();//提取图像UIGraphicsEndImageContext();returnnewImage;}@end总结:是b最好让设计师给你剪个全套,省去你的麻烦。不过,这样也好,可以节省app的体积,也可以节省按钮高亮、选择等图片。