iOS开发图片纯色填充(两种方式)和缩小图片

UIImage图标换色和变换大小

这里的图片大都指只有两个色的图标,有时候我们的图标需要根据不同的状态显示不同的颜色,有一种简单的处理方式是,将不同状态的颜色图标都切出来,变换状态的时候直接使用相应的颜色图标,这样会导致资源图标增多,安装包变大。还有就是使用比较流行的iconFont模式。最后也可以让我们编码人员直接改变图标的颜色。

方法一、使用tint颜色改变(最简单的)

要求:你的明白tint的概念和使用

a.如果你是将图标导如到Assets.xcassets文件夹,可以选择将你要改变颜色的图标的render as 属性设为 模板图片(原本图标你应该保留一份),操作如图



之后在代码逻辑中使用该模板图片,【重点】设置tintColor即可,记得设置哟,不然就是系统的蓝色拉。

b.如果你是直接将图片导入到项目,就需要用代码,代码如下:

var image1 = UIImage(named:"test") //原图
        var templateImage = image1?.withRenderingMode(.alwaysTemplate) //模型图(可以改变颜色了)
        //然后根据情况将需要的图片加到视图中。
        //【注意】需要设置视图的tintColor值,否则就会用系统的或者父视图的tint色
        //如:self.imageView.image = templateImage ; self.imageView.tintColor = .red
建议使用b.可以保留者原图。

方法二、通过代码再画布上下文中设置。

a.Swift参考代码

/**
    //对图片着色
    //参数:blendMode,参考下面链接
    */
    func changeColor(_ color:UIColor, blendMode:CGBlendMode) -> UIImage {
        //方式一(可以)
        UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.main.scale)
        let context = UIGraphicsGetCurrentContext()
        color.setFill()
        //移动图片
        context!.translateBy(x: 0, y: self.size.height)
        context!.scaleBy(x: 1.0, y: -1.0)
        
        let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
        context!.draw(self.cgImage!, in: rect)
        //模式配置
        context!.setBlendMode(blendMode)
        context!.addRect(rect)
        context!.drawPath(using: CGPathDrawingMode.fill)
        //创建获取图片
        let coloredImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return coloredImage!
}
BlendMode参数值解释 (不一定适用于OC)

b. OC参考代码片段:

//纯色化图片

- (UIImage *)setImage:(UIImage *)image toColor:(UIColor *)color{
    
    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, image.size.height);
    /没有这部分图片会跳动
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    /
    CGContextClipToMask(context, rect, image.CGImage);
    [color setFill];
    CGContextFillRect(context, rect);
    UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return colorImage;
}


将图片缩小化


swift版本

/**
     *改变图片大小(缩小)
     */
    func changeSize(_ size:CGSize) -> UIImage {
        UIGraphicsBeginImageContext(size)
        self.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage!
    }


OC版本

//缩小图片
- (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)size{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    _testImage = newImage;
    return newImage;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值