[Swift]iOS开发: 图片翻转、切割圆角、调整大小

extension UIImage{
    //调整大小
    func scaleImageToSize(_ size:CGSize) -> UIImage{
UIGraphicsBeginImageContextWithOptions(size,false,UIScreen.main.scale)      
        self.draw(in: CGRect(x: 0,y: 0, width: size.width, height: size.height))
        let reSizeImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return reSizeImage
    }

    //切割圆角
    func clip(radius:CGFloat)->UIImage{
UIGraphicsBeginImageContextWithOptions(CGSize(width:radius*2,height:radius*2), false, UIScreen.main.scale)
        let ctx = UIGraphicsGetCurrentContext()
        ctx?.addArc(center: CGPoint(x:radius,y:radius), radius: radius, startAngle: 0, endAngle: .pi/180 * 360, clockwise: true)
        ctx?.clip()
        self.draw(in: CGRect(origin: .zero, size: CGSize(width: radius*2, height: radius*2)))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }

    //翻转
    func flip()->UIImage{
        UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.main.scale)
        let ctx = UIGraphicsGetCurrentContext()
        ctx?.scaleBy(x: -1, y: 1)
        ctx?.translateBy(x: -self.size.width, y: 0)
        self.draw(in: CGRect(origin: .zero, size: self.size))
        let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }
}
[Swift]iOS开发: 图片翻转、切割圆角、调整大小_第1张图片

你可能感兴趣的:([Swift]iOS开发: 图片翻转、切割圆角、调整大小)