Using GCD to thread the favicon generation and redrawing, for smooth performance.

This commit is contained in:
Samuel Clay 2011-10-18 08:56:13 -07:00
parent 0f5b60ee1d
commit 81f3f4f05a
3 changed files with 50 additions and 23 deletions

View file

@ -649,22 +649,28 @@ viewForHeaderInSection:(NSInteger)section {
NSDictionary *results = [[NSDictionary alloc]
initWithDictionary:[responseString JSONValue]];
for (id feed_id in results) {
NSDictionary *feed = [appDelegate.dictFeeds objectForKey:feed_id];
[feed setValue:[results objectForKey:feed_id] forKey:@"favicon"];
[appDelegate.dictFeeds setValue:feed forKey:feed_id];
NSString *favicon = [feed objectForKey:@"favicon"];
if ((NSNull *)favicon != [NSNull null] && [favicon length] > 0) {
NSData *imageData = [NSData dataWithBase64EncodedString:favicon];
UIImage *faviconImage = [UIImage imageWithData:imageData];
[Utilities saveImage:faviconImage feedId:feed_id];
// [imageCache setObject:faviconImage forKey:feed_id];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
for (id feed_id in results) {
NSDictionary *feed = [appDelegate.dictFeeds objectForKey:feed_id];
[feed setValue:[results objectForKey:feed_id] forKey:@"favicon"];
[appDelegate.dictFeeds setValue:feed forKey:feed_id];
NSString *favicon = [feed objectForKey:@"favicon"];
if ((NSNull *)favicon != [NSNull null] && [favicon length] > 0) {
NSData *imageData = [NSData dataWithBase64EncodedString:favicon];
UIImage *faviconImage = [UIImage imageWithData:imageData];
[Utilities saveImage:faviconImage feedId:feed_id];
}
}
}
[Utilities saveimagesToDisk];
dispatch_sync(dispatch_get_main_queue(), ^{
[results release];
[self.feedTitlesTable reloadData];
});
});
[results release];
[self.feedTitlesTable reloadData];
}
- (void)requestFailed:(ASIHTTPRequest *)request {

View file

@ -8,9 +8,13 @@
#import <Foundation/Foundation.h>
@interface Utilities : NSObject
@interface Utilities : NSObject <NSCacheDelegate> {
NSCache *imageCache;
}
+ (void)saveImage:(UIImage *)image feedId:(NSString *)filename;
+ (UIImage *)getImage:(NSString *)filename;
+ (void)saveimagesToDisk;
@end

View file

@ -10,24 +10,25 @@
@implementation Utilities
static NSCache *imageCache;
static NSMutableDictionary *imageCache;
+ (void)saveImage:(UIImage *)image feedId:(NSString *)filename {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];
NSString *path = [cacheDirectory stringByAppendingPathComponent:filename];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:path atomically:YES];
if (!imageCache) {
imageCache = [[NSMutableDictionary dictionary] retain];
}
// Save image to memory-based cache, for performance when reading.
// NSLog(@"Saving %@", [imageCache allKeys]);
[imageCache setObject:image forKey:filename];
}
+ (UIImage *)getImage:(NSString *)filename {
UIImage *image;
image = [imageCache objectForKey:filename];
if (!image) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
// Image not in cache, search on disk.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];
NSString *path = [cacheDirectory stringByAppendingPathComponent:filename];
@ -41,4 +42,20 @@ static NSCache *imageCache;
}
}
+ (void)saveimagesToDisk {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_async(queue, [[^{
for (id filename in imageCache) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];
NSString *path = [cacheDirectory stringByAppendingPathComponent:filename];
// Save image to disk
UIImage *image = [imageCache objectForKey:filename];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:path atomically:YES];
}
} copy] autorelease]);
}
@end