Skip to content

Instantly share code, notes, and snippets.

@AlexFlyce
Last active May 22, 2018 14:35
Show Gist options
  • Save AlexFlyce/285c76649ea7d2b1f85f4172ef2efcbe to your computer and use it in GitHub Desktop.
Save AlexFlyce/285c76649ea7d2b1f85f4172ef2efcbe to your computer and use it in GitHub Desktop.
ART001-decodedImage.swift
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