iOS 图片压缩 (尺寸大小)
/// 压缩图片
/// - Parameters:
/// - size: 目标尺寸
/// - image: 当前图片
/// - Returns: 目标图片
@objc public class func scaleImage(size:CGSize,image:UIImage) ->UIImage{
if __CGSizeEqualToSize(size, image.size) {
returnimage
}
letwidth = image.size.width
letheight = image.size.height
letscale = width/height
varnewWidth:CGFloat=0.0;
varnewHeigth:CGFloat=0.0
if size.width>size.height{
newWidth = size.width
newHeigth = newWidth/scale
}else if size.width==size.height{
ifwidth>height {
newWidth = size.width
newHeigth = newWidth/scale
}else if width==height {
newWidth = size.width
newHeigth = size.height
}else{
newHeigth = size.height
newWidth = newHeigth*scale
}
}else{
newHeigth = size.height
newWidth = newHeigth*scale
}
let rect:CGRect=CGRect(x:0, y:0, width: newWidth, height: newHeigth)
UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.main.scale)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext() ?? image
UIGraphicsEndImageContext()
return newImage
}