此问题已在此处有答案:
How do I resize the UIImage to reduce upload image size(21个回答)
6年前关闭。
我想减少像这个网站http://resizeimage.net图像的大小我的图像是1080 x 360大小120kb后,我用下面的代码,但当我使用的网站,我得到58kb或如果有一个库或算法来压缩JPEG文件
func resizeImage(_ image: UIImage, newHeight: CGFloat) -> UIImage {
let scale = newHeight / image.size.height
let newWidth = image.size.width * scale
UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
let imageData = UIImageJPEGRepresentation(newImage!, 0.5)! as Data
UIGraphicsEndImageContext()
return UIImage(data:imageData)!
}
1条答案
按热度按时间whlutmcx1#
传递给
UIImageJPEGRepresentation
的压缩质量决定了JPEG图像的图像质量。如果您的压缩质量为0.5,请尝试更低的压缩质量,例如0.1:Apple的Docs:
compressionQuality
:生成的JPEG图像的质量,表示为0.0到1.0之间的值。值0.0表示最大压缩(或最低质量),而值1.0表示最小压缩(或最佳质量)。