加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift压缩图片以及修改图片颜色

发布时间:2020-12-14 06:21:11 所属栏目:百科 来源:网络整理
导读:extension UIImage { func scaleToSize(size:CGSize) - UIImage { UIGraphicsBeginImageContextWithOptions(size,false, 0 ) self .draw ( in : CGRect( x : 0 , y : 0 ,width: size .width ,height: size .height )) let img = UIGraphicsGetImageFromCurre
extension UIImage {
    func scaleToSize(size:CGSize) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size,false,0)
        self.draw(in: CGRect(x: 0,y: 0,width: size.width,height: size.height))
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img!
    }


    func changeColor(color:UIColor) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.size,self.scale)
        let context = UIGraphicsGetCurrentContext()
        context?.translateBy(x: 0,y: self.size.height)
        context?.scaleBy(x: 1.0,y: -1.0)//kCGBlendModeNormal
        context?.setBlendMode(.normal)
        let rect = CGRect(x: 0,width: self.size.width,height: self.size.height)
        context?.clip(to: rect,mask: self.cgImage!);
        color.setFill()
        context?.fill(rect)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage!
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读