以编程方式从底部裁剪图像 [英] Crop image from bottom programmatically

查看:59
本文介绍了以编程方式从底部裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义相机应用程序.事情进展顺利.

I am working on the custom camera application. Things are working good.

但是我从底部裁剪图像时遇到了问题.也就是说,裁剪后的图像的宽度与原始图像的宽度完全相同,但是高度将是原始图像的1/3,并且必须从底部开始.

But I got into the problem while cropping the image from bottom. i.e. cropped image is having exact same width as the original image but height will be 1/3 of the original image and it must start from the bottom.

推荐答案

Swift 3解决方案:

Swift 3 solution:

func cropBottomImage(image: UIImage) -> UIImage {
    let height = CGFloat(image.size.height / 3)
    let rect = CGRect(x: 0, y: image.size.height - height, width: image.size.width, height: height)
    return cropImage(image: image, toRect: rect)
}

裁切矩形的辅助方法:

func cropImage(image:UIImage, toRect rect:CGRect) -> UIImage{
    let imageRef:CGImage = image.cgImage!.cropping(to: rect)!
    let croppedImage:UIImage = UIImage(cgImage:imageRef)
    return croppedImage
}

这篇关于以编程方式从底部裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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