{"id":27039700,"url":"https://github.com/mattdavo/uiimagedefaults","last_synced_at":"2025-04-05T03:18:02.878Z","repository":{"id":56924910,"uuid":"175621982","full_name":"mattDavo/UIImageDefaults","owner":"mattDavo","description":"Simple Swift extension for locally saving and loading images with ease.","archived":false,"fork":false,"pushed_at":"2019-03-18T09:57:20.000Z","size":142,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-05T01:17:53.192Z","etag":null,"topics":["cocoapods","ios","swift","swift-library","uiimage","uiimageview","userdefaults"],"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/mattDavo.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}},"created_at":"2019-03-14T12:55:50.000Z","updated_at":"2021-07-15T18:14:46.000Z","dependencies_parsed_at":"2022-08-20T22:50:27.644Z","dependency_job_id":null,"html_url":"https://github.com/mattDavo/UIImageDefaults","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattDavo%2FUIImageDefaults","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattDavo%2FUIImageDefaults/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattDavo%2FUIImageDefaults/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattDavo%2FUIImageDefaults/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mattDavo","download_url":"https://codeload.github.com/mattDavo/UIImageDefaults/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247280319,"owners_count":20912976,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["cocoapods","ios","swift","swift-library","uiimage","uiimageview","userdefaults"],"created_at":"2025-04-05T03:18:01.972Z","updated_at":"2025-04-05T03:18:02.863Z","avatar_url":"https://github.com/mattDavo.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# UIImageDefaults\nSimple Swift extension for locally saving and loading images with ease.\n\n## Installation\n### Cocoa Pods\nYou can use [CocoaPods](https://cocoapods.org/) to install `UIImageDefaults` by adding it to your Podfile:\n```ruby\npod 'UIImageDefaults', '~\u003e 1.0.0'\n```\n\n## Usage\n\n### Import the package to use with an image\n```Swift\n\nimport UIImageDefaults\n\n// Let's say we have an image that we have downloaded from somewhere:\nvar image = downloadImage()\n```\n### User Defaults\n```Swift\n// If we would like to use this image in our app all the time without having to download it every time,\n// we can save it to the UserDefaults.\nlet userDefaults = UserDefaults()\n\n// We can save (and override) the image to user defaults:\nuserDefaults.set(value: image, forKey: \"myImage\")\n\n// and then restore it from user defaults:\nimage = userDefaults.uiimage(forKey: \"myImage\")\n\n// Then delete it from the cache, so that we aren't taking up too much room:\nuserDefaults.removeImage(forKey: \"myImage\")\n\n// Or remove all images in our cache:\nuserDefaults.removeImages()\n\n```\n### UIImage\n```Swift\n// Instead of interfacing through the UserDefaults, we can also interface directly with the UIImage class:\nimage.saveImage(forKey: \"myImage\")            // Saving\nimage = UIImage.getImage(forKey: \"myImage\")   // Loading\nimage = UIImage(forKey: \"myImage\")            // Initialising\nUIImage.removeImage(forKey: \"myImage\")        // Removing\nUIImage.removeImages()                        // Removing all\n\n```\n### UIImageView\n```Swift\n// Furthermore, we can also cleanly interact with the UIImageView class to load and store images without hassle.\n// We can easily load into a UIImageView from a URL.\nlet url = \"https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png\"\nlet imageView = UIImageView(frame: view.bounds)\n\n// loadCachedImage(withUrl, checkForUpdates) will load from the cache if it is available, and then if\n// checkForUpdates is true it will update the image view will the latest content from the URL. If the\n// image is not in the cache, it will default to loading from the URL. Each time the image is loaded\n// from the URL, it will be saved to the cached with the (modified( URL as the key.\nimageView.loadCachedImage(withUrl: url, checkForUpdates: true)\n\n// loadCachedImage(withKey, checkForUpdates, withLoader) operates with the same logic except the user\n// is able to provide their own UIImage loading logic. The caching will operate the same, so long as\n// the user calls the completion handler correctly on failure and success, providing either the image\n// on success or a nil value on failure.\nimageView.loadCachedImage(withKey: \"google\", checkForUpdates: true) { (done: @escaping (UIImage?) -\u003e Void) in\n    URLSession.shared.dataTask(with: URL(string: url)!) {\n        (data: Data?, resposnse: URLResponse?, error: Error?) -\u003e Void in\n        guard let data = data, error == nil else {\n            // Call the completion handler when you've failed\n            done(nil)\n            return\n        }\n        DispatchQueue.main.async {\n            // And call the completion handler when you've succeded\n            done(UIImage(data: data))\n        }\n    }.resume()\n}\n\n// Now this looks a little confusing and you won't want your code looking that everywhere, but that's\n// ok because there is a solution. If you are loading your images in the same way we can easily wrap\n// that into a function to make your loadCachedImage(...) code short and simple.\n// Using the same, load from url example, we can create a function, customImageLoader:\nfunc customImageLoader(url: String) -\u003e ((@escaping (UIImage?) -\u003e()) -\u003e Void) {\n    return { (done: @escaping (UIImage?) -\u003e Void) in\n        URLSession.shared.dataTask(with: URL(string: url)!) {\n            (data: Data?, resposnse: URLResponse?, error: Error?) -\u003e Void in\n            guard let data = data, error == nil else {\n                // Call the completion handler when you've failed\n                done(nil)\n                return\n            }\n            DispatchQueue.main.async {\n                // And call the completion handler when you've succeded\n                done(UIImage(data: data))\n            }\n        }.resume()\n    }\n}\n\n// Then we can easily resuse this code everywhere:\nimageView.loadCachedImage(withKey: \"google\", checkForUpdates: true, withLoader: customImageLoader(url: url))\nlet url2 = \"https://avatars0.githubusercontent.com/u/9919?s=280\u0026v=4\"\nimageView2.loadCachedImage(withKey: \"github\", checkForUpdates: true, withLoader: customImageLoader(url: url2))\n```\n\n## License\nAvailable under the [MIT License](https://github.com/mattDavo/Lingo/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattdavo%2Fuiimagedefaults","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattdavo%2Fuiimagedefaults","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattdavo%2Fuiimagedefaults/lists"}