【发布时间】:2018-07-19 22:33:50
【问题描述】:
我正在使用这个库的裁剪功能来裁剪图像,就像 Instagram 一样。 (https://github.com/fahidattique55/FAImageCropper) 它的代码裁剪部分是这样工作的。
private func captureVisibleRect() -> UIImage {
var croprect = CGRect.zero
let xOffset = (scrollView.imageToDisplay?.size.width)! / scrollView.contentSize.width;
let yOffset = (scrollView.imageToDisplay?.size.height)! / scrollView.contentSize.height;
croprect.origin.x = scrollView.contentOffset.x * xOffset;
croprect.origin.y = scrollView.contentOffset.y * yOffset;
let normalizedWidth = (scrollView?.frame.width)! / (scrollView?.contentSize.width)!
let normalizedHeight = (scrollView?.frame.height)! / (scrollView?.contentSize.height)!
croprect.size.width = scrollView.imageToDisplay!.size.width * normalizedWidth
croprect.size.height = scrollView.imageToDisplay!.size.height * normalizedHeight
let toCropImage = scrollView.imageView.image?.fixImageOrientation()
let cr: CGImage? = toCropImage?.cgImage?.cropping(to: croprect)
let cropped = UIImage(cgImage: cr!)
return cropped }
但问题是,例如我有一张 (800(W)*600(H)) 尺寸的照片,我想通过使用全缩小来裁剪它的全宽。此函数计算croprect 变量 (800( W)*800(H)) 正确。但是在这部分代码let cr: CGImage? = toCropImage?.cgImage?.cropping(to: croprect) 之后,cr 的分辨率变为(800(W)*600(H))。如何通过用白色填充它的空白部分来将其转换为方形图像?
【问题讨论】:
-
你为什么不尝试使用 UIGraphicsContext 绘制一个具有所需宽度和高度的 CGRect,将其填充颜色设置为白色,然后将调整大小的图像放在上面。
-
@Anwuna 你有什么代码可以实现这个吗?
-
你不需要图书馆。裁剪并不难,并且已经在 Stack Overflow 上进行了彻底的解释。在询问之前尝试搜索。