Skip to content

Instantly share code, notes, and snippets.

@iosdevie
Created May 26, 2021 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iosdevie/0a685717801070582ef9210117370fa2 to your computer and use it in GitHub Desktop.
Save iosdevie/0a685717801070582ef9210117370fa2 to your computer and use it in GitHub Desktop.
func clippedImageForRect(clipRect: CGRect, inView view: UIView) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(clipRect.size, true, UIScreen.main.scale)
if let ctx = UIGraphicsGetCurrentContext(){
ctx.translateBy(x: -clipRect.origin.x, y: -clipRect.origin.y);
view.layer.render(in: ctx)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment