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

人走茶凉 提交于 2019-11-30 01:57:42

Using Swift 3

Image cropping can be done using CGImages from CoreGraphics.

Get the CGImage version of a UIImage like this:

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

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

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

Finally, convert back from CGImage to 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.

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.

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

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

Hope this helps.

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

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

Get the CGImage from UIImage:

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

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/cicontext if 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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!