Skip to content

Instantly share code, notes, and snippets.

@onmyway133
Last active July 5, 2018 11:10
Show Gist options
  • Save onmyway133/54e790ee056809f507f9003711d0530b to your computer and use it in GitHub Desktop.
Save onmyway133/54e790ee056809f507f9003711d0530b to your computer and use it in GitHub Desktop.
private func cropImage(image: UIImage, normalisedRect: CGRect) -> UIImage? {
let x = normalisedRect.origin.x * image.size.width
let y = normalisedRect.origin.y * image.size.height
let width = normalisedRect.width * image.size.width
let height = normalisedRect.height * image.size.height
let rect = CGRect(x: x, y: y, width: width, height: height).scaleUp(scaleUp: 0.1)
guard let cropped = image.cgImage?.cropping(to: rect) else {
return nil
}
let croppedImage = UIImage(cgImage: cropped, scale: image.scale, orientation: image.imageOrientation)
return croppedImage
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment