Skip to content

Instantly share code, notes, and snippets.

@Lucien
Created April 10, 2017 14:24
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 Lucien/ee463429f852d1a7cf5376e54fc1cf01 to your computer and use it in GitHub Desktop.
Save Lucien/ee463429f852d1a7cf5376e54fc1cf01 to your computer and use it in GitHub Desktop.
Crop UIImage
import Foundation
extension UIImage {
func crop(rect: CGRect) -> UIImage? {
var scaledRect = rect
scaledRect.origin.x *= scale
scaledRect.origin.y *= scale
scaledRect.size.width *= scale
scaledRect.size.height *= scale
guard let imageRef: CGImage = cgImage?.cropping(to: scaledRect) else {
return nil
}
return UIImage(cgImage: imageRef, scale: scale, orientation: imageOrientation)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment