         guard let data = sessionCachedData(withURL: url) else {
             return nil
         }
-        guard let image = UIImage(data: data) else {
+        guard let image = createImage(data: data) else {
             return nil
         }
         addToMemoryCache(image, url: url)

        let key = cacheKeyForURL(url)
        let variant = variantForURL(url)
        let fileURL = permanentCacheFileURL(key: key, variant: variant)
        guard let image = UIImage(contentsOfFile: fileURL.path) else {
            return nil
        }
        addToMemoryCache(image, url: url)

        memoryCache.setObject(image, forKey: identifier, cost: Int(image.size.width * image.size.height))
    }
    
    fileprivate func createImage(data: Data) -> UIImage? {
        guard let source = CGImageSourceCreateWithData(data as CFData, nil), CGImageSourceGetCount(source) > 0 else {
            return nil
        }
        let options = [kCGImageSourceShouldCache as String: NSNumber(value: true)] as CFDictionary
        guard let cgImage = CGImageSourceCreateImageAtIndex(source, 0, options) else {
            return nil
        }
        let image = UIImage(cgImage: cgImage)
        return image
    }
    
    fileprivate func createImage(fileURL: URL) -> UIImage? {
        guard let source = CGImageSourceCreateWithURL(fileURL as CFURL, nil), CGImageSourceGetCount(source) > 0 else {
            return nil
        }
        let options = [kCGImageSourceShouldCache as String: NSNumber(value: true)] as CFDictionary
        guard let cgImage = CGImageSourceCreateImageAtIndex(source, 0, options) else {
            return nil
        }
        let image = UIImage(cgImage: cgImage)
        return image
    }
    
    public func permanentlyCachedImage(withURL url: URL) -> UIImage? {
        if let memoryCachedImage = memoryCachedImage(withURL: url) {
            return memoryCachedImage
