{"id":15594358,"url":"https://github.com/orta/imagecachingexamples","last_synced_at":"2026-04-01T23:41:00.625Z","repository":{"id":66647099,"uuid":"56864832","full_name":"orta/ImageCachingExamples","owner":"orta","description":"A complete example of using SDWebImage to do synchronous image loading for FBSnapshots","archived":false,"fork":false,"pushed_at":"2016-04-22T15:37:22.000Z","size":1047,"stargazers_count":18,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-06T01:41:48.199Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/orta.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-04-22T15:17:01.000Z","updated_at":"2022-03-02T10:35:05.000Z","dependencies_parsed_at":"2023-05-25T20:45:38.279Z","dependency_job_id":null,"html_url":"https://github.com/orta/ImageCachingExamples","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/orta/ImageCachingExamples","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orta%2FImageCachingExamples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orta%2FImageCachingExamples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orta%2FImageCachingExamples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orta%2FImageCachingExamples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/orta","download_url":"https://codeload.github.com/orta/ImageCachingExamples/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orta%2FImageCachingExamples/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31013964,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-27T02:58:54.984Z","status":"ssl_error","status_checked_at":"2026-03-27T02:58:46.993Z","response_time":164,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T00:38:52.190Z","updated_at":"2026-03-27T03:15:25.076Z","avatar_url":"https://github.com/orta.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ImageCachingExamples\n\n### Problem\n\nWe want to take snapshots of view controllers that have images in them, like this\n\n\u003cimg width=\"50%\" src= \"screenshots/example.png\"\u003e\n\nThe thing is, that is not going to be a small PNG file, and that image downloading is asynchronous. This means that we could end up with slow tests. Boo. Hiss.\n\nOne answer to this problem is to work with your caching library in order to end up with snapshots that look like this:\n\n\u003cimg width=\"50%\" src= \"ImageCachingExamplesTests/ReferenceImages/ViewControllerTests/looks_right@3x.png\"\u003e\n\nBut how do you do that?\n\n### TLDR Solution\n\nTake your view controller, and where you normally have something like this\n\n``` swift\nclass ViewController: UIViewController {\n\n    @IBOutlet weak var primaryImageView: UIImageView!\n    @IBOutlet weak var secondaryImageView: UIImageView!\n    @IBOutlet weak var tertiaryImageView: UIImageView!\n\n    let imageOneAddress = \"[...]\"\n    let imageTwoAddress = \"[...]\"\n    let imageThreeAddress = \"[...]\"\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n\n        // Take the addresses and set the imageview's images asycnhronously\n        primaryImageView.setImageWithURL(NSURL(string:imageOneAddress)!)\n        secondaryImageView.setImageWithURL(NSURL(string:imageTwoAddress)!)\n        tertiaryImageView.setImageWithURL(NSURL(string:imageThreeAddress)!)\n    }\n}\n\n```\n\nMake an extension on `UIImageView` like so:\n\n``` swift\nimport UIKit\nimport SDWebImage\n\nextension UIImageView {\n\n    func or_setImageWithURL(url: NSURL) {\n\n        // Set a coloured background while it loads\n        if (image == nil) {\n            image = UIImage.imageWithColor(.lightGrayColor())\n        }\n\n        // Pass directly to SDWebImage if not in testing environment\n        guard NSClassFromString(\"XCTest\") != nil else {\n            sd_setImageWithURL(url)\n            return\n        }\n\n        // Look inside the SD Image Cache to see if our URL has already been stored\n        let imageManager = SDWebImageManager.sharedManager()\n        let key = imageManager.cacheKeyForURL(url)\n\n        // If not, provide a useful error message, or optionally raise an exception\n        guard let cachedImage = imageManager.imageCache.imageFromMemoryCacheForKey(key) else {\n            print(\"Detected a un-stubbed image request for URL: \\(url)\")\n            sd_setImageWithURL(url)\n            return\n        }\n\n        // Synchronously set the cached image\n        image = cachedImage\n    }\n}\n```\n\nDon't worry about the `UIImage.imageWithColor()` calls they just generate images from a colour and cache them, the [source for that is here](https://github.com/orta/ImageCachingExamples/blob/master/ImageCachingExamplesTests/UIImage%2BColor.swift). The important part is the we ask the cache directly for an image and set it - if the class `XCTest` exists.\n\nNext in your tests create an easy way to pre-cache your urls with an image. I use colours because you can easily differentiate each UIImageView and it's going to save space in the PNG.\n\n``` swift\nimport Quick\nimport Nimble\nimport Nimble_Snapshots\nimport SDWebImage\nimport UIKit\n\n@testable\nimport ImageCachingExamples\n\n/// Takes an address, and a colour and caches it for synchronous testing\nfunc cacheColoredImageForURL(string: String, color: UIColor) {\n    let image = UIImage.imageWithColor(color)\n    SDWebImageManager.sharedManager().saveImageToCache(image, forURL:NSURL(string: string)!)\n}\n\nclass ViewControllerTests: QuickSpec {\n    override func spec() {\n        var subject: ViewController!\n\n        beforeEach {\n            // This sort of thing can be made more elegant with\n            // https://github.com/AliSoftware/SwiftGen\n\n            let storyboard = UIStoryboard(name: \"Main\", bundle: nil)\n            subject = storyboard.instantiateInitialViewController() as! ViewController\n\n            // Use our coloured images in the SDWebCache\n            cacheColoredImageForURL(subject.imageOneAddress, color: UIColor.greenColor())\n            cacheColoredImageForURL(subject.imageTwoAddress, color: UIColor.blueColor())\n            cacheColoredImageForURL(subject.imageThreeAddress, color: UIColor.redColor())\n        }\n\n        it(\"looks right\") {\n            // Simple snapshot test, to record I did:\n            // expect(subject) == recordSnapshot()\n\n            expect(subject) == snapshot()\n        }\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Forta%2Fimagecachingexamples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Forta%2Fimagecachingexamples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Forta%2Fimagecachingexamples/lists"}