All Projects → mjmsmith → Czsharedimage

mjmsmith / Czsharedimage

Licence: mit
Optimize iOS image loading by sharing UIImage objects.

Projects that are alternatives of or similar to Czsharedimage

Ioniconskit
Use Ionicons in your Swift projects.
Stars: ✭ 310 (+2284.62%)
Mutual labels:  cocoapods, uiimage
Tocropviewcontroller
A view controller for iOS that allows users to crop portions of UIImage objects
Stars: ✭ 4,210 (+32284.62%)
Mutual labels:  cocoapods, uiimage
Sablurimageview
You can use blur effect and it's animation easily to call only two methods.
Stars: ✭ 538 (+4038.46%)
Mutual labels:  cocoapods, uiimage
Styledecorator
Easy string decoration with styles
Stars: ✭ 17 (+30.77%)
Mutual labels:  cocoapods
Loaderbutton
LoaderButton is a very interesting animation loading button.
Stars: ✭ 19 (+46.15%)
Mutual labels:  cocoapods
Taniwhatextfield
My first cocoapod framework
Stars: ✭ 26 (+100%)
Mutual labels:  cocoapods
Vtacknowledgementsviewcontroller
Acknowledgements screen displaying a list of licenses, for example from CocoaPods dependencies.
Stars: ✭ 863 (+6538.46%)
Mutual labels:  cocoapods
Webhere
HTML scraping for Objective-C.
Stars: ✭ 16 (+23.08%)
Mutual labels:  cocoapods
Ppnumberbutton
iOS中一款高度可定制性商品计数按钮(京东/淘宝/饿了么/美团外卖/百度外卖样式)
Stars: ✭ 845 (+6400%)
Mutual labels:  cocoapods
Cascadingtabledelegate
A no-nonsense way to write cleaner UITableViewDelegate and UITableViewDataSource in Swift.
Stars: ✭ 931 (+7061.54%)
Mutual labels:  cocoapods
Swipemenuviewcontroller
Swipable tab and menu View and ViewController.
Stars: ✭ 926 (+7023.08%)
Mutual labels:  cocoapods
Imgix Swift
A Swift client library for generating URLs with imgix
Stars: ✭ 19 (+46.15%)
Mutual labels:  cocoapods
Mixpanel Iphone
iPhone tracking library for Mixpanel Analytics
Stars: ✭ 939 (+7123.08%)
Mutual labels:  cocoapods
Preferences
⚙ Add a preferences window to your macOS app in minutes
Stars: ✭ 898 (+6807.69%)
Mutual labels:  cocoapods
Ios Uiimage Render To Pdf
iOS Render UIImage to PDF and merging PDF files
Stars: ✭ 8 (-38.46%)
Mutual labels:  uiimage
Jwrefreshcontrol
A refresh control(header & footer of UIScrollview) for iOS app.
Stars: ✭ 17 (+30.77%)
Mutual labels:  cocoapods
Tbactionsheet
A Custom&Powerful Action Sheet For iOS. 一个 ActionSheet 满足所有样式!超高自由度的可定制!
Stars: ✭ 942 (+7146.15%)
Mutual labels:  cocoapods
Swipeabletabbarcontroller
UITabBarController with swipe interaction between its tabs.
Stars: ✭ 919 (+6969.23%)
Mutual labels:  cocoapods
Cheatyxml
CheatyXML is a Swift framework designed to manage XML easily
Stars: ✭ 23 (+76.92%)
Mutual labels:  cocoapods
Hdcommontools
一句代码即可实现多种常用功能,根据数据处理、文件管理、多媒体管理、权限管理、系统信息、Appstore操作、加密解密、快捷宏定义等几种不同的类型封装,A short code can achieve a variety of commonly used functions,according to the data processing, file management, multimedia management, rights management, information system, Appstore, encryption and decryption, quick macro definition type package several different Category
Stars: ✭ 25 (+92.31%)
Mutual labels:  cocoapods

CZSharedImage

On iOS, the UIImage method imageNamed: optimizes image loading in two ways:

  1. It caches recently loaded UIImage objects to avoid reloading them.
  2. Multiple requests for the same named image get a reference to the same UIImage object.

The first optimization saves (loading) time at the cost of (memory) space.

For 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.

CZSharedImage is a tiny library that provides the second optimization for images loaded using UIImage#imageWithContentsOfFile: and UIImage#imageWithData:.

Note 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.

Usage

CZSharedImage requires ARC and iOS 6.0. (It uses the NSMapTable class introduced in 6.0.)

Import the header file CZSharedImage.h.

For usage examples, see CZSharedImageTests.m or the example app.

In 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:.

CZSharedImage class

Shared images are created using the CZSharedImage class.

Creating images

+ (UIImage *)imageWithContentsOfFile:(NSString *)path;
+ (UIImage *)imageWithData:(NSData *)data;
+ (UIImage *)imageWithData:(NSData *)data scale:(CGFloat)scale;

Simply 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.

UIImage *image = [CZSharedImage imageWithContentsOfFile:@"/path/to/image/file"];

Note 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.

Associating images with paths

+ (UIImage *)imageForPath:(NSString *)path;
+ (void)setImage:(UIImage *)image forPath:(NSString *)path;

For 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:

// Try to get a reference to this image.

NSString *path = @"http://example.com/foo.png";
UIImage *image = [CZSharedImage imageForPath:path];

// If not available, fetch the image and associate it with the URL.
// (Error handling not shown.)

if (!image) {
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];
  [NSURLConnection sendAsynchronousRequest:request
                                     queue:[NSOperationQueue mainQueue]
                         completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
                         {
                             // While the fetched UIImage object below remains alive,
                             // CZSharedImage#imageForPath: will return a reference to it.

                             UIImage *image = [UIImage imageWithData:data];
                             [CZSharedImage setImage:image forPath:path];
                             ...
                         }];
}
...

Similarly, an image fetched from a database could be associated with a unique path corresponding to the database row (and column if necessary):

// Try to get a reference to this image.

int personid = <...get row ID...>;
NSString *path = [NSString stringWithFormat:@"db:/person/%d", personid];
UIImage *image = [CZSharedImage imageForPath:path];

// If not available, fetch the image and associate it with the path.
// (Error handling not shown.)

if (!image) {
  NSData *data = <...load image data...>;
  
  // While the fetched UIImage object below remains alive,
  // CZSharedImage#imageForPath: will return a reference to it.
  
  UIImage *image = [UIImage imageWithData:data];
  [CZSharedImage setImage:image forPath:path];
}
...
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].