Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tonymillion/tmdiskcache
a disk cache with network loading and UIImage category
https://github.com/tonymillion/tmdiskcache
Last synced: 2 months ago
JSON representation
a disk cache with network loading and UIImage category
- Host: GitHub
- URL: https://github.com/tonymillion/tmdiskcache
- Owner: tonymillion
- Created: 2013-01-02T14:22:18.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2014-09-28T15:52:52.000Z (about 10 years ago)
- Last Synced: 2024-05-09T09:56:16.139Z (8 months ago)
- Language: Objective-C
- Size: 208 KB
- Stars: 15
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
TMDiskCache
===========A disk cache with network loading and UIImageView category
The main active parts of this are TMDiskCache and TMDownloadManager.
The part you'll want to use is the UIImageView category which allows you to set the image to load from a URL.
**Because of the loading animation you will need to link your project with QuarzCore.framework**
# Using the UIImageView category
Very basically you call it like this
```
[self.userPhoto loadFromURL:displayedPost.user.avatar.imageURL
placeholderImage:[UIImage imageNamed:@"noprofilepic"]
fromCache:[AppDelegate sharedAppDelegate].avatarImageCache];
```In this example we are setting the image on a UIImageView called userPhoto the placeholder will be displayed while the image is downloaded & decoded, here we specifically use the avatarImageCache we created earlier, if you pass nil here the UIImageView will use the TMDiskCache singleton.
*CAVEAT*
The UIImageView category now uses method swizzling to take care of this.
~~Because of the Category there is a requrirement that if you wish to load a normal image into the imageview, you need to call~~
#####If Loading network images is all you care about, and you dont care about per type caches, read no further, and simply pass nil into the fromCache parameter!
#basic usage of TMDiskCache
At its simplest the UIImageView category will use the "default" TMDiskCache singleton, however a better implementation is for you to alloc you own instances of TMDiskCache with their own cache sizes, e.g. if you were writing an app.net client you could do something like
appDelegate.h
```
@property(strong,readonly) TMDiskCache *postImageCache;
@property(strong,readonly) TMDiskCache *coverImageCache;
@property(strong,readonly) TMDiskCache *avatarImageCache;
```in the .m
```
_avatarImageCache = [[TMDiskCache alloc] initWithCacheName:@"profilePictureCache" andCacheSize:5];
_postImageCache = [[TMDiskCache alloc] initWithCacheName:@"postImageCache" andCacheSize:50];
_coverImageCache = [[TMDiskCache alloc] initWithCacheName:@"coverArtCache" andCacheSize:20];
```The cacheSize is always defined in megabytes.
Next, the TMDiskCache has a built-in cache trim algorithm, this is called like ```[_coverArtImageCache trimCache];```
You should call this in your applications ```- (void)applicationDidEnterBackground:(UIApplication *)application``` method e.g:
```
[_avatarImageCache trimCache];
[_postImageCache trimCache];
[_coverImageCache trimCache];
```To determine if an item is in the cache you call:
```
-(void)checkCacheForURL:(NSURL*)remoteURL
success:(void(^)(NSURL * localURL))success
failure:(void(^)(NSURL * localURL, NSError * error))failure
```This will check if the item for the remote URL exists in the cache, if it does the URL to the item is passed as a parameter to the success block, if not the failure block is called (and again what *WOULD* be the local URL is passed in, along with an error).
If the item is in the cache it will be 'touched' so that it wont be expired from the disk cache immediately.
There are other methods in this class (one to set a data blob for a URL and one to test for exsitence & load as a NSData blob). These operate on a background Queue for nice smooth operation.
# Basic usage of TMDownloadManager
**to be written**