Skip to content

Instantly share code, notes, and snippets.

@haashem
Created February 15, 2017 08:05
Show Gist options
  • Save haashem/34a7f00892a3b9f71358ccb57b5f4d55 to your computer and use it in GitHub Desktop.
Save haashem/34a7f00892a3b9f71358ccb57b5f4d55 to your computer and use it in GitHub Desktop.
an extension on UIImage for decompressing it before assigning to UIImageView. Recommended to run on a background thread
//
// UIImage+Decompress.swift
//
// Created by Hashem Aboonajmi on 15/02/2017
//
import UIKit
extension UIImage {
var decompressed: UIImage {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
let bitmapContext = CGContext(data: nil, width: Int(rect.size.width), height: Int(rect.size.height), bitsPerComponent: cgImage!.bitsPerComponent, bytesPerRow: cgImage!.bytesPerRow, space: cgImage!.colorSpace!, bitmapInfo: cgImage!.bitmapInfo.rawValue)
bitmapContext?.draw(cgImage!, in: rect)
let decompressedImageRef: CGImage = bitmapContext!.makeImage()!
let decompressedImage = UIImage(cgImage: decompressedImageRef)
return decompressedImage
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment