Created
April 10, 2017 14:24
-
-
Save Lucien/ee463429f852d1a7cf5376e54fc1cf01 to your computer and use it in GitHub Desktop.
Crop UIImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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