Last active
May 22, 2018 14:35
-
-
Save AlexFlyce/285c76649ea7d2b1f85f4172ef2efcbe to your computer and use it in GitHub Desktop.
ART001-decodedImage.swift
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
extension UIImage { | |
class func decodedImage(_ image: UIImage) -> UIImage? { | |
guard let newImage = image.cgImage else { return nil } | |
// To optimize this, you can some cache control. | |
let colorspace = CGColorSpaceCreateDeviceRGB() | |
let context = CGContext(data: nil, | |
width: newImage.width, | |
height: newImage.height, | |
bitsPerComponent: 8, | |
bytesPerRow: newImage.width * 4, | |
space: colorspace, | |
bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue) | |
context?.draw(newImage, in: CGRect(x: 0, y: 0, width: newImage.width, height: newImage.height)) | |
let drawnImage = context?.makeImage() | |
if let drawnImage = drawnImage { | |
return UIImage(cgImage: drawnImage) | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment