// // Utilities.m // NewsBlur // // Created by Samuel Clay on 10/17/11. // Copyright (c) 2011 NewsBlur. All rights reserved. // #import "Utilities.h" @implementation Utilities static NSMutableDictionary *imageCache; + (void)saveImage:(UIImage *)image feedId:(NSString *)filename { if (!imageCache) { imageCache = [NSMutableDictionary dictionary]; } // Save image to memory-based cache, for performance when reading. // NSLog(@"Saving %@", [imageCache allKeys]); if (image) { [imageCache setObject:image forKey:filename]; } else { NSLog(@"%@ has no image!!!", filename); } } + (UIImage *)getImage:(NSString *)filename { return [self getImage:filename isSocial:NO]; } + (UIImage *)getImage:(NSString *)filename isSocial:(BOOL)isSocial { UIImage *image; image = [imageCache objectForKey:filename]; if (!image) { // Image not in cache, search on disk. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cacheDirectory = [paths objectAtIndex:0]; NSString *path = [cacheDirectory stringByAppendingPathComponent:filename]; image = [UIImage imageWithContentsOfFile:path]; } if (image) { return image; } else { if (isSocial) { return [UIImage imageNamed:@"user.png"]; } else { return [UIImage imageNamed:@"world.png"]; } } } + (void)saveimagesToDisk { dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0); dispatch_async(queue, [^{ for (NSString *filename in [imageCache allKeys]) { 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]; [UIImagePNGRepresentation(image) writeToFile:path atomically:YES]; } } copy]); } + (UIImage *)roundCorneredImage: (UIImage*) orig radius:(CGFloat) r { UIGraphicsBeginImageContextWithOptions(orig.size, NO, 0); [[UIBezierPath bezierPathWithRoundedRect:(CGRect){CGPointZero, orig.size} cornerRadius:r] addClip]; [orig drawInRect:(CGRect){CGPointZero, orig.size}]; UIImage* result = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return result; } @end