SWIFT:如何在不降低质量的情况下缩小UIGraphics / imageContext?

时间:2017-11-13 22:48:58

标签: swift uigraphicscontext

我有一个用户点按的框以绘制圆圈。在屏幕上,图形很锋利,看起来很棒。我还让用户能够通过电子邮件发送或发送图形文字,但质量非常差。我还必须在正确的位置将盒子画回到盒子上。

盒子资源是@ 2x 908×534像素和@ 3x 1362×801像素。我想缩小图像,所以我不必发送这么大的PNG文件。

let hockeyNetImageView   = UIImageView(frame: CGRect(x: 0, y: 0, width: 340, height: 200))
hockeyNetImageView.image = UIImage(named: "hockeyNet")

UIGraphicsBeginImageContext(CGSize(width: 340, height: 200))

let context: CGContext = UIGraphicsGetCurrentContext()!
hockeyNetImageView.layer.render(in: context)
let img = UIGraphicsGetImageFromCurrentImageContext()!

UIGraphicsEndImageContext()

//Resize the image
let size = (img.size).applying(CGAffineTransform(scaleX: 1.0, y: 1.0))

let hasAlpha = true
let scale: CGFloat = 0.0

UIGraphicsBeginImageContextWithOptions((img.size), !hasAlpha, scale)
img.draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: size))

let scaleImage = UIGraphicsGetImageFromCurrentImageContext()

goalieNetArray.append(UIImagePNGRepresentation(scaleImage!)!)

UIGraphicsEndImageContext()

0 个答案:

没有答案