{"id":22828747,"url":"https://github.com/mjmsmith/czsharedimage","last_synced_at":"2025-04-23T16:24:42.935Z","repository":{"id":12363044,"uuid":"15012154","full_name":"mjmsmith/czsharedimage","owner":"mjmsmith","description":"Optimize iOS image loading by sharing UIImage objects.","archived":false,"fork":false,"pushed_at":"2014-05-02T02:03:58.000Z","size":248,"stargazers_count":14,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-23T16:24:32.388Z","etag":null,"topics":["cocoapods","uiimage"],"latest_commit_sha":null,"homepage":"","language":"Objective-C","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/mjmsmith.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":"2013-12-07T20:13:27.000Z","updated_at":"2025-02-24T07:12:38.000Z","dependencies_parsed_at":"2022-09-13T20:10:34.664Z","dependency_job_id":null,"html_url":"https://github.com/mjmsmith/czsharedimage","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mjmsmith%2Fczsharedimage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mjmsmith%2Fczsharedimage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mjmsmith%2Fczsharedimage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mjmsmith%2Fczsharedimage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mjmsmith","download_url":"https://codeload.github.com/mjmsmith/czsharedimage/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250468781,"owners_count":21435539,"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","uiimage"],"created_at":"2024-12-12T19:12:01.576Z","updated_at":"2025-04-23T16:24:42.912Z","avatar_url":"https://github.com/mjmsmith.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CZSharedImage\n\nOn iOS, the __UIImage__ method ```imageNamed:``` optimizes image loading in two ways:\n\n1. It caches recently loaded UIImage objects to avoid reloading them.\n2. Multiple requests for the same named image get a reference to the same UIImage object.\n\nThe first optimization saves (loading) time at the cost of (memory) space.\n\nFor images that currently exist as UIImage objects in the running application, the second optimization saves both time and space;  there's no need to load or decode the image, and there aren't duplicate copies each occupying memory.\n\n__CZSharedImage__ is a tiny library that provides the second optimization for images loaded using ```UIImage#imageWithContentsOfFile:``` and ```UIImage#imageWithData:```.\n\nNote that __CZSharedImage__ is not a cache; it simply tracks live UIImage objects.  Its value is in cases where an image is used multiple times in a view (accessory images in a table view, for instance), or multiple times in a navigation hierarchy.\n\n## Usage\n\n__CZSharedImage__ requires ARC and iOS 6.0.  (It uses the NSMapTable class introduced in 6.0.)\n\nImport the header file __CZSharedImage.h__.\n\nFor usage examples, see [CZSharedImageTests.m](https://github.com/mjmsmith/czsharedimage/blob/master/CZSharedImageTests/CZSharedImageTests.m) or the [example app](https://github.com/mjmsmith/czsharedimage/tree/master/CZSharedImageExample).\n\nIn the example app, 100 instances of a 500x500 image (each occupying approximately 1MB of memory when decoded) are displayed on the screen.  The resident size of the app when using  ```UIImage#imageWithContentsOfFile:``` is 99MB more than when using ```CZSharedImage#imageWithContentsOfFile:```.\n\n## CZSharedImage class\n\nShared images are created using the __CZSharedImage__ class.\n\n### Creating images\n\n```objc\n+ (UIImage *)imageWithContentsOfFile:(NSString *)path;\n+ (UIImage *)imageWithData:(NSData *)data;\n+ (UIImage *)imageWithData:(NSData *)data scale:(CGFloat)scale;\n```\n\nSimply replace calls to the above three __UIImage__ methods with the __CZSharedImage__ methods of the same name.  If the associated __UIImage__ object already exists in the running app, the method will immediately return a reference to that object.  If not, it will be loaded as usual by the corresponding __UIImage__ initializer.\n\n```objc\nUIImage *image = [CZSharedImage imageWithContentsOfFile:@\"/path/to/image/file\"];\n```\n\nNote that in the case of images loaded using ```imageWithData:```, subsequent requests are matched using an MD5 hash of the data.  A more efficient mechanism is to manually associate the image with a chosen path; see the next section for details.\n\n### Associating images with paths\n\n```objc\n+ (UIImage *)imageForPath:(NSString *)path;\n+ (void)setImage:(UIImage *)image forPath:(NSString *)path;\n```\n\nFor cases where an image is loaded using some mechanism other than the three __imageWith...__ methods (for instance, over the network), it can still be manually associated with a path such as the URL for subsequent requests:\n\n```objc\n// Try to get a reference to this image.\n\nNSString *path = @\"http://example.com/foo.png\";\nUIImage *image = [CZSharedImage imageForPath:path];\n\n// If not available, fetch the image and associate it with the URL.\n// (Error handling not shown.)\n\nif (!image) {\n  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];\n  [NSURLConnection sendAsynchronousRequest:request\n                                     queue:[NSOperationQueue mainQueue]\n                         completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)\n                         {\n                             // While the fetched UIImage object below remains alive,\n                             // CZSharedImage#imageForPath: will return a reference to it.\n\n                             UIImage *image = [UIImage imageWithData:data];\n                             [CZSharedImage setImage:image forPath:path];\n                             ...\n                         }];\n}\n...\n```\n\nSimilarly, an image fetched from a database could be associated with a unique path corresponding to the database row (and column if necessary):\n\n```objc\n// Try to get a reference to this image.\n\nint personid = \u003c...get row ID...\u003e;\nNSString *path = [NSString stringWithFormat:@\"db:/person/%d\", personid];\nUIImage *image = [CZSharedImage imageForPath:path];\n\n// If not available, fetch the image and associate it with the path.\n// (Error handling not shown.)\n\nif (!image) {\n  NSData *data = \u003c...load image data...\u003e;\n  \n  // While the fetched UIImage object below remains alive,\n  // CZSharedImage#imageForPath: will return a reference to it.\n  \n  UIImage *image = [UIImage imageWithData:data];\n  [CZSharedImage setImage:image forPath:path];\n}\n...\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmjmsmith%2Fczsharedimage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmjmsmith%2Fczsharedimage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmjmsmith%2Fczsharedimage/lists"}