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, false, 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, y: 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!
}
}
Swift压缩图片以及修改图片颜色
最新推荐文章于 2021-12-22 15:03:51 发布