EGOUICache on GitHub:
http://github.com/enormego/EGOUICache/tree/masterAuthors:
EGOUICache
Created by enormego
EGOUICache is a collection of functions written on top of EGOCache to cache intensive drawRect: methods. EGOUICache will cache these methods for you, and return a UIImage that you can quickly draw. EGOUICache should not be used in UITableViewCell's with data that is constantly change (like a Twitter app, for example). It's intended to be used in areas where the data is pretty much set, but you need the option for it to change, giving you a dynamically generated, "static" cache.
Methods
EGOUICache is powered by EGOCache, but it's written using C functions since they're faster and speed is what we're trying to achieve here.
UIImage* EGOUICachedDrawing(id target, SEL selector, CGRect rect, NSString* salt);- Caches the selector performed on target, this is the primary function and should be used with UIImage's drawInRect method
- Parameters
target: usually self, target that selector will be performed onselector: selector that will draw the "fresh" contentrect: Rect to draw insalt: Unique string for the content in this cached drawing.
EGOUICacheSetCacheTimeoutInterval(NSTimeInterval cacheTimeoutInterval);- This should be called prior to calling
EGOUICachedDrawing(), or within it as long as you're not nesting cache calls. - Parameters
cacheTimeoutIntervalnumber of seconds the cache should persist for
- Convenience methods
EGOUICacheSetCacheTimeoutOneYear();EGOUICacheSetCacheTimeoutOneMonth();EGOUICacheSetCacheTimeoutOneWeek();EGOUICacheSetCacheTimeoutOneDay();EGOUICacheSetCacheTimeoutOneHour();
- This should be called prior to calling
EGOUICacheCancel();- Cancels one level of caching. This should be called if data hasn't yet loaded or something isn't what it's expected to be. The image returned by
EGOUICachedDrawing()will be nil.
- Cancels one level of caching. This should be called if data hasn't yet loaded or something isn't what it's expected to be. The image returned by
Example
Let's say you have a button that gets drawn a bunch of times, that uses intensive blending, and really only needs to be drawn once since the text rarely changes:
- (void)drawRect:(CGRect)rect {
EGOUICacheSetCacheTimeoutOneMonth();
[EGOUICachedDrawing(self, @selector(freshDrawRect:), rect, self.currentTitle) drawInRect:rect];
}
- (void)freshDrawRect:(CGRect)rect {
UIFont* font = [UIFont boldSystemFontOfSize:22.0f];
CGRect textRect;
textRect.size = [text sizeWithFont:font];
textRect.origin.x = floorf((rect.size.width - titleSize.width) / 2);
textRect.origin.y = floorf((rect.size.height - titleSize.height) / 2) + 1.0f;
// A bunch of complicated code that draws the text with a gradient, and glow around it, with a 1px shadow
}
The EGOUICachedDrawing() call in drawRect will check to see if it has a cache for the target/selector with the salt being the current title, if it does, it returns the UIImage right away. If it doesn't detect the cache, it calls freshDrawRect:, and then caches and returns an image with the contents drawn in freshDrawRect:.
License
EGOUICache is available under the MIT license:
Copyright (c) 2009-2010 enormego
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- Activity
- shaun commented on issue 2 on… Thursday
- shaun commented on issue 2 on… Thursday
- shaun commented on issue 2 on… Thursday
- shaun commented on issue 2 on… Thursday
- shaun merged pull request 13… Apr 26
- shaun merged pull request 12… Apr 19
- shaun merged pull request 23… Apr 17
- shaun merged pull request 14… Apr 17
- shaun merged pull request 13… Apr 17
- shaun opened pull request 10… Apr 4
- View all of our open source projects
- Tweet Tweet
- Anyone have any experience wi… Apr 28
- Go check out @gifture from ou… Apr 26
- RT @saveriomondelli: Today is… Apr 26
- RT @saveriomondelli: If anyon… Apr 25
- @noel_llopis Credit goes to @… Apr 25
- @steipete Nope, order page sa… Apr 25
- @noel_llopis http://t.co/cWX7… Apr 25
- I wouldn't normally be concer… Apr 25
- Did anyone order a WWDC ticke… Apr 25
- RT @whitneyhess: I want to li… Apr 19
- Follow us on twitter
- About Us
- We develop awesome applications for the iPhone, Mac and Web. Our applications are powerful, simple and elegant. This website is our attempt at giving back a little bit of what we've learned over the years. Hope you enjoy it!
- Check out enormego.com for more!
-