【问题标题】:Swift how to crop image with always 1:1 aspect ratioSwift如何以始终1:1的纵横比裁剪图像
【发布时间】: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 上进行了彻底的解释。在询问之前尝试搜索。

标签: ios swift image crop


【解决方案1】:

在此过程之后,您可以使用此链接中的答案对图像进行平方。 How to draw full UIImage inside a square with white color on the edge

这是它的 Swift 3 版本。

private func squareImageFromImage(image: UIImage) -> UIImage{
    var maxSize = max(image.size.width,image.size.height)
    var squareSize = CGSize.init(width: maxSize, height: maxSize)

    var dx = (maxSize - image.size.width) / 2.0
    var dy = (maxSize - image.size.height) / 2.0
    UIGraphicsBeginImageContext(squareSize)
    var rect = CGRect.init(x: 0, y: 0, width: maxSize, height: maxSize)

    var context = UIGraphicsGetCurrentContext()
    context?.setFillColor(UIColor.white.cgColor)
    context?.fill(rect)

    rect = rect.insetBy(dx: dx, dy: dy)
    image.draw(in: rect, blendMode: CGBlendMode.normal, alpha: 1.0)
    var squareImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return squareImage!
}

【讨论】:

    【解决方案2】:

    我建议您使用 UIGraphicsContext 绘制一个具有预期宽度和高度的矩形,并用所需的颜色填充它。然后在其上绘制裁剪后的图像。

    我尚未对此进行测试,但这应该可以满足您的需求。

    我省略了您代码的其他部分以专注于基本要素。

    ....
    let context: CGContext? = UIGraphicsGetCurrentContext()
    let rect = CGRect(x: 0, y: 0, width: width, height: height)
    let color = UIColor.white
    color.setFill()
    context?.fill(rect)
    
    let cr: CGImage? = toCropImage?.cgImage?.cropping(to: croprect)
    let cropped = UIImage(cgImage: cr!)
    
    context?.draw(cropped, in: rect)
    let newImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage!
    

    将宽度和高度替换为所需的宽度和高度。

    【讨论】:

    • 非常感谢,我会试一试的。
    • 感谢您的意见,但此代码无法创建正确的 newImage 变量。但我已经通过使用您的意见解决了这个问题,非常感谢。
    • 代码只是一个粗略的示例,说明如何让您入门,而不是一个工作部分。我的目的是让您修改它以满足您自己的特定需求。
    • 非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多