Created
March 10, 2018 06:55
-
-
Save tatetate55/4c1f1ac0fde1a530e6264fa57afc666a to your computer and use it in GitHub Desktop.
UIImage+Extension
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 UIKit | |
extension UIImage { | |
func cropping(to: CGRect) -> UIImage? { | |
var opaque = false | |
if let cgImage = cgImage { | |
switch cgImage.alphaInfo { | |
case .noneSkipLast, .noneSkipFirst: | |
opaque = true | |
default: | |
break | |
} | |
} | |
UIGraphicsBeginImageContextWithOptions(to.size, opaque, scale) | |
draw(at: CGPoint(x: -to.origin.x, y: -to.origin.y)) | |
let result = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment