ios 如何使用 Swift 选择图像的一部分、裁剪并保存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31254435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:45:05  来源:igfitidea点击:

How to select a portion of an image, crop, and save it using Swift?

iosimagecamerauiimagepickercontrollercrop

提问by Sriram

I am trying to create an iOS app using Swift to capture images and let the user save a selected portion of the image. In many cam based apps, I noticed that a rectangular frame is offered to let the users choose the desired portion. This involves either sliding the edges of the rectangle or moving the corners to fit the required area.

我正在尝试使用 Swift 创建一个 iOS 应用程序来捕获图像并让用户保存图像的选定部分。在许多基于 cam 的应用程序中,我注意到提供了一个矩形框架,让用户选择所需的部分。这涉及滑动矩形的边缘或移动角以适应所需的区域。

Could you please guide me on how to implement that moveable rectangle and how to save only that piece of the image?

你能指导我如何实现那个可移动的矩形以及如何只保存那块图像吗?

采纳答案by Sriram

Found one more solution. This time it is in Swift. The solution looks elegant and the code relative to other such solutions is written in fewer number of lines.

找到了另一种解决方案。这次是在 Swift 中。该解决方案看起来很优雅,并且相对于其他此类解决方案的代码用更少的行数编写。

Here it is.. https://github.com/DuncanMC/CropImgThanks to Duncan Champney for making his work available on github.

这是.. https://github.com/DuncanMC/CropImg感谢 Duncan Champney 在 github 上提供他的工作。

回答by Paulo

Using Swift 3

使用 Swift 3

Image cropping can be done using CGImagesfrom CoreGraphics.

图像裁剪是可以做到用CGImagesCoreGraphics中

Get the CGImage version of a UIImage like this:

像这样获取 UIImage 的 CGImage 版本:

// cgImage is an attribute of UIImage
let cgImage = image.cgImage

CGImage objects have a method cropping(to: CGRect)that does the cropping:

CGImage 对象有一个方法cropping(to: CGRect)来进行裁剪:

let croppedCGImage: CGImage = cgImage.cropping(to: toRect)

Finally, convert back from CGImageto UIImage:

最后,转换回CGImage的UIImage

let uiImage = UIImage(cgImage: croppedCGImage)

Example function:

示例函数:

func cropImage(image: UIImage, toRect: CGRect) -> UIImage? {
    // Cropping is available trhough CGGraphics
    let cgImage :CGImage! = image.cgImage
    let croppedCGImage: CGImage! = cgImage.cropping(to: toRect)

    return UIImage(cgImage: croppedCGImage)
}

The CGRect attribute of cropping defines the 'crop rectangle' inside the image that will be cropped.

裁剪的 CGRect 属性定义了将被裁剪的图像内的“裁剪矩形”。

回答by Subbu

https://github.com/myang-git/iOS-Image-Crop-Viewdoes something like what you are looking for..

https://github.com/myang-git/iOS-Image-Crop-View做一些你正在寻找的东西..

Hope this helps.

希望这可以帮助。

回答by Iraniya Naynesh

If you getting issue like rotating 90 after cropping image try this.
Store the original image scale and orientation property for later Use

如果您在裁剪图像后遇到类似旋转 90 度的问题,请尝试此操作。
存储原始图像的比例和方向属性供以后使用

let imgOrientation = image?.imageOrientation
let imgScale = image?.scale

Get the CGImage from UIImage:

从 UIImage 获取 CGImage:

let cgImage = image.cgImage

Pass the cropArea(CGRect) area you want to crop (incase you are using imageView.image you have find the scale and do maths to find cgRect) adding that code below if needed

传递您想要裁剪的cropArea(CGRect) 区域(如果您正在使用imageView.image,您已经找到了比例并进行数学运算以找到cgRect),如果需要,在下面添加该代码

let croppedCGImage = cgImage.cropping(to: cropArea)
let coreImage = CIImage(cgImage: croppedCGImage!)

Need context for rendering image (its a heavy process do check https://developer.apple.com/documentation/coreimage/cicontextif you are doing multiple times ) using this you can set the scale and orientation we created in first line so image don't rotate 90

需要渲染图像的上下文(如果你多次这样做,它是一个繁重的过程,请检查https://developer.apple.com/documentation/coreimage/cicontext)使用它你可以设置我们在第一行创建的比例和方向,因此图像不要旋转90

let ciContext = CIContext(options: nil)
let filteredImageRef = ciContext.createCGImage(coreImage, from: coreImage.extent)
let finalImage = UIImage(cgImage:filteredImageRef!, scale:imgScale!, orientation:imgOrientation!)
imageView.image = finalImage

回答by Daniel Hu

I was looking for an answer for iOS 13, Swift 5, and here comes the savior library: CropViewController.

我一直在寻找适用于 iOS 13、Swift 5 的答案,而救世主库来了:CropViewController

It has almost every feature you can dream of: cropping at any aspect ratio, image rotation, reset to original image, translucent background. Also it is easy to install (Cocoapod, Carthage) and very easy to use. Remember to check out their demo sample app included in the repo.

它几乎具有您梦寐以求的所有功能:以任何纵横比裁剪、图像旋转、重置为原始图像、半透明背景。它也易于安装(Cocoapod、Carthage)并且非常易于使用。记得查看他们包含在 repo 中的演示示例应用程序。