Skip to content

Instantly share code, notes, and snippets.

@tatetate55
Created March 10, 2018 06:55
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 tatetate55/4c1f1ac0fde1a530e6264fa57afc666a to your computer and use it in GitHub Desktop.
Save tatetate55/4c1f1ac0fde1a530e6264fa57afc666a to your computer and use it in GitHub Desktop.
UIImage+Extension
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