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

前端之家收集整理的这篇文章主要介绍了Swift压缩图片以及修改图片颜色前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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!
    }
}
原文链接:https://www.f2er.com/swift/321894.html

猜你在找的Swift相关文章