Created
March 24, 2017 11:06
-
-
Save mindbrix/6ca50e3234bf74912a92a134f91fca88 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// UIImage+Decompression.swift | |
// Contis Protocol | |
// | |
// Created by Nigel Barber on 01/03/2017. | |
// Copyright © 2017 @mindbrix. All rights reserved. | |
// | |
import CoreGraphics | |
import ImageIO | |
import UIKit | |
extension UIImage { | |
static func decompressedCGImageWithData(data: NSData) -> CGImageRef? { | |
if let source = CGImageSourceCreateWithData(data as CFData, nil) { | |
if let image = CGImageSourceCreateImageAtIndex(source, 0, nil) { | |
let width = CGImageGetWidth(image), height = CGImageGetHeight(image) | |
let BGRA = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue | CGBitmapInfo.ByteOrder32Little.rawValue) | |
if let ctx = CGBitmapContextCreate(nil, width, height, 8, width * 4, CGColorSpaceCreateDeviceRGB(), BGRA.rawValue) { | |
CGContextSetBlendMode(ctx, .Copy) | |
CGContextSetInterpolationQuality(ctx, .None) | |
CGContextDrawImage(ctx, CGRectMake(0, 0, CGFloat(width), CGFloat(height)), image) | |
return CGBitmapContextCreateImage(ctx) | |
} | |
} | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment