本文介绍了在swift3中将图像缩放到更小的尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将正在图形编程的图像缩放到照片库中。我正在尝试缩放图像以使其更小。如何使用下面的代码缩小图像
I am trying to scale a image that is being graphic programmed into the photo gallery. I am trying to scale the image to make it smaller. How would I use the code below to make the image smaller
image1.scale
推荐答案
使用此扩展程序调整图片大小:
Use this extension to resize your image:
extension UIImage{
func resizeImageWith(newSize: CGSize) -> UIImage {
let horizontalRatio = newSize.width / size.width
let verticalRatio = newSize.height / size.height
let ratio = max(horizontalRatio, verticalRatio)
let newSize = CGSize(width: size.width * ratio, height: size.height * ratio)
UIGraphicsBeginImageContextWithOptions(newSize, true, 0)
draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: newSize))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}
基本上你是计算宽高比以在调整大小时保持图像完好无损。然后,您将获取当前图像上下文并将其绘制到指定的矩形中,最后将其作为新图像返回。
Basically you are calculating the aspect ratio to keep the image intact while resizing it. Then you are getting the current image context and drawing it in the specified rectangle and finally returning it as a new image.
这篇关于在swift3中将图像缩放到更小的尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!