斯威夫特 4.1
这就是我调整图像大小的方式。您可以在显示之前对其进行处理。
// Resize to ~1.5Kx2K resoultion and compress to <200KB (JPEG 0.2)
private func resizePhoto(_ originalPhoto: UIImage) -> UIImage? {
var size: CGSize
let scale = UIScreen.main.scale
if originalPhoto.size.width > originalPhoto.size.height { // Landscape
size = CGSize(width: 2016/scale, height: 1512/scale)
} else { // Portrait
size = CGSize(width: 1512/scale, height: 2016/scale)
}
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
originalPhoto.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let resizedPhoto = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let scaledPhotoData = UIImageJPEGRepresentation(resizedPhoto!, 0.2)
//print(">>> Resized data size: \(scaledPhotoData!.count)")
return UIImage(data: scaledPhotoData)
}