如何在 swift 4 或更高版本中裁剪具有可选区域的图像? [英] How to crop an image with a selectable area in swift 4 or later?

查看:44
本文介绍了如何在 swift 4 或更高版本中裁剪具有可选区域的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些关于我想在我的应用中实现的功能的帮助.我在 Aspect Fit 中有一个带有内容模式的图像视图.当我从我的库中获取图像时,我想用可调整的矩形裁剪一个区域,创建一个新图像.我已经寻找了一些示例或在线教程,但没有成功.任何人都可以帮助我吗?这是我的视图中的图像.

I need some help with a function that I'd like to implement in my app. I have a view with an image view with content mode in Aspect Fit. When I get an image from my library I would like to crop an area with an adjustable rectangle creating a new image. I've looked for some exemple or online tutorial but I did not succeed. Can anyone help me with that? Here are the images from my View.

.

推荐答案

简单的解决方案是在特定的 CGRect 中渲染图像视图:

The simple solution is to just render the image view within a particular CGRect:

func snapshot(in imageView: UIImageView, rect: CGRect) -> UIImage {
    return UIGraphicsImageRenderer(bounds: rect).image { _ in
        imageView.drawHierarchy(in: imageView.bounds, afterScreenUpdates: true)
    }
}

这种方法的局限性在于,如果图像的分辨率远高于图像视图所能渲染的分辨率(我们使用纵横比例拟合"时经常出现这种情况),您将失去这种额外的精度.

The limitation of that approach is that if the image is a considerably higher resolution than the image view could render (as is often the case when we use "aspect scale fit"), you’ll lose this additional precision.

如果你想保留分辨率,你应该将 CGRect 转换为与图像的坐标,在这种情况下,假设纵横比例适合"(即居中和缩放,使整个图像是显示):

If you want to preserve the resolution, you should convert the CGRect to coordinates with the image, in this case, assuming "aspect scale fit" (namely, centered and scaled so the whole image is shown):

func snapshot(in imageView: UIImageView, rect: CGRect) -> UIImage {
    assert(imageView.contentMode == .scaleAspectFit)

    let image = imageView.image!

    // figure out what the scale is

    let imageRatio = imageView.bounds.width / imageView.bounds.height
    let imageViewRatio = image.size.width / image.size.height

    let scale: CGFloat
    if imageRatio > imageViewRatio {
        scale = image.size.height / imageView.bounds.height
    } else {
        scale = image.size.width / imageView.bounds.width
    }

    // convert the `rect` into coordinates within the image, itself

    let size = rect.size * scale
    let origin = CGPoint(x: image.size.width  / 2 - (imageView.bounds.midX - rect.minX) * scale,
                         y: image.size.height / 2 - (imageView.bounds.midY - rect.minY) * scale)
    let scaledRect = CGRect(origin: origin, size: size)

    // now render the image and grab the appropriate rectangle within
    // the image’s coordinate system

    let format = UIGraphicsImageRendererFormat()
    format.scale = image.scale
    format.opaque = false

    return UIGraphicsImageRenderer(bounds: scaledRect, format: format).image { _ in
        image.draw(at: .zero)
    }
}

使用此扩展程序:

extension CGSize {
    static func * (lhs: CGSize, rhs: CGFloat) -> CGSize {
        return CGSize(width: lhs.width * rhs, height: lhs.height * rhs)
    }
}

结果:

这篇关于如何在 swift 4 或更高版本中裁剪具有可选区域的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆