裁剪UIImage到中心广场 [英] Crop UIImage to center square

查看:128
本文介绍了裁剪UIImage到中心广场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我获得了1920x1080分辨率的图像。我试图将它们裁剪到中心广场区域(即中心的1080x1080平方)。该决议可能在未来发生变化。是否有可能以这种方式裁剪图像?我已尝试在SO上发布了一些东西,但如果我尝试裁剪然后居中,我总会得到660x1080的图像。这是描绘我想要实现的目标的图像。

I get images that are in 1920x1080 resolution. I am attempting to crop them to the center square area (ie 1080x1080 square in the center). The resolution may change in the future. Would it be possible to crop the image this way? I have tried a couple things posted on SO, but I always get an image that is 660x1080 if I try to crop then center. Here is an image depicting what I want to achieve.

基本上红色是原始的,绿色是我想要的,黄色只是显示mixX和midY线。

Basically the red is original, green is what I want, and yellow is just showing mixX and midY lines.

我试过的代码。

func imageByCroppingImage(image: UIImage, toSize size: CGSize) -> UIImage{
    let newCropWidth: CGFloat?
    let newCropHeight: CGFloat?

    if image.size.width < image.size.height {
        if image.size.width < size.width {
            newCropWidth = size.width
        } else {
            newCropWidth = image.size.width
        }
        newCropHeight = (newCropWidth! * size.height) / size.width
    } else {
        if image.size.height < size.height {
            newCropHeight = size.height
        } else {
            newCropHeight = image.size.height
        }
        newCropWidth = (newCropHeight! * size.width) / size.height
    }

    let x = image.size.width / 2.0 - newCropWidth! / 2.0
    let y = image.size.height / 2.0 - newCropHeight! / 2.0

    let cropRect = CGRect(x: x, y: y, width: newCropWidth!, height: newCropHeight!)
    let imageRef = image.cgImage!.cropping(to: cropRect)

    let cropped = UIImage(cgImage: imageRef!, scale: 1.0, orientation: .right)
    return cropped
}

然后我将该方法传递给CGSize(1080,1080)。我得到的图像是660,1080。有什么想法吗?

And then I pass that method a CGSize(1080, 1080). I get an image that is 660, 1080. Any thoughts?

目的是裁剪图像,而不是只显示图像。

推荐答案

您可以使用我在回答不带 UIBezierPath(ovalIn:breadthRect).addClip()

extension UIImage {
    var isPortrait:  Bool    { return size.height > size.width }
    var isLandscape: Bool    { return size.width > size.height }
    var breadth:     CGFloat { return min(size.width, size.height) }
    var breadthSize: CGSize  { return CGSize(width: breadth, height: breadth) }
    var breadthRect: CGRect  { return CGRect(origin: .zero, size: breadthSize) }
    var squared: UIImage? {
        UIGraphicsBeginImageContextWithOptions(breadthSize, false, scale)
        defer { UIGraphicsEndImageContext() }
        guard let cgImage = cgImage?.cropping(to: CGRect(origin: CGPoint(x: isLandscape ? floor((size.width - size.height) / 2) : 0, y: isPortrait  ? floor((size.height - size.width) / 2) : 0), size: breadthSize)) else { return nil }
        UIImage(cgImage: cgImage).draw(in: breadthRect)
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}







let imageURL = URL(string: "https://www.sample.com/yourImage.jpg")!
let image = UIImage(data: try! Data(contentsOf: imageURL))!
let squared = image.squared

这篇关于裁剪UIImage到中心广场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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