Skip to content

Instantly share code, notes, and snippets.

@mindbrix
Created March 24, 2017 11:06
Show Gist options
  • Save mindbrix/6ca50e3234bf74912a92a134f91fca88 to your computer and use it in GitHub Desktop.
Save mindbrix/6ca50e3234bf74912a92a134f91fca88 to your computer and use it in GitHub Desktop.
//
// 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