2011-10-17 09:28:15 -07:00
|
|
|
//
|
|
|
|
// Utilities.m
|
|
|
|
// NewsBlur
|
|
|
|
//
|
|
|
|
// Created by Samuel Clay on 10/17/11.
|
|
|
|
// Copyright (c) 2011 NewsBlur. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "Utilities.h"
|
|
|
|
|
|
|
|
@implementation Utilities
|
|
|
|
|
2011-10-18 08:56:13 -07:00
|
|
|
static NSMutableDictionary *imageCache;
|
2011-10-17 09:28:40 -07:00
|
|
|
|
|
|
|
+ (void)saveImage:(UIImage *)image feedId:(NSString *)filename {
|
2011-10-18 08:56:13 -07:00
|
|
|
if (!imageCache) {
|
2012-07-15 15:06:06 -07:00
|
|
|
imageCache = [NSMutableDictionary dictionary];
|
2011-10-18 08:56:13 -07:00
|
|
|
}
|
2011-10-17 09:28:40 -07:00
|
|
|
|
2011-10-18 08:56:13 -07:00
|
|
|
// Save image to memory-based cache, for performance when reading.
|
|
|
|
// NSLog(@"Saving %@", [imageCache allKeys]);
|
2012-07-12 00:10:42 -07:00
|
|
|
if (image) {
|
|
|
|
[imageCache setObject:image forKey:filename];
|
|
|
|
} else {
|
|
|
|
NSLog(@"%@ has no image!!!", filename);
|
|
|
|
}
|
2011-10-17 09:28:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (UIImage *)getImage:(NSString *)filename {
|
2012-07-18 15:26:55 -07:00
|
|
|
return [self getImage:filename isSocial:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (UIImage *)getImage:(NSString *)filename isSocial:(BOOL)isSocial {
|
2011-10-17 09:28:40 -07:00
|
|
|
UIImage *image;
|
|
|
|
image = [imageCache objectForKey:filename];
|
2011-10-18 08:56:13 -07:00
|
|
|
|
2011-10-17 09:28:40 -07:00
|
|
|
if (!image) {
|
2011-10-18 08:56:13 -07:00
|
|
|
// Image not in cache, search on disk.
|
|
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
|
2011-10-17 09:28:40 -07:00
|
|
|
NSString *cacheDirectory = [paths objectAtIndex:0];
|
|
|
|
NSString *path = [cacheDirectory stringByAppendingPathComponent:filename];
|
|
|
|
|
|
|
|
image = [UIImage imageWithContentsOfFile:path];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (image) {
|
|
|
|
return image;
|
|
|
|
} else {
|
2012-07-18 15:26:55 -07:00
|
|
|
if (isSocial) {
|
|
|
|
return [UIImage imageNamed:@"user.png"];
|
|
|
|
} else {
|
|
|
|
return [UIImage imageNamed:@"world.png"];
|
|
|
|
}
|
|
|
|
|
2011-10-17 09:28:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-18 08:56:13 -07:00
|
|
|
+ (void)saveimagesToDisk {
|
|
|
|
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
|
|
|
|
|
2012-07-15 15:06:06 -07:00
|
|
|
dispatch_async(queue, [^{
|
2012-06-26 11:45:42 -07:00
|
|
|
for (NSString *filename in [imageCache allKeys]) {
|
2011-10-18 08:56:13 -07:00
|
|
|
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];
|
2012-07-15 16:46:46 -07:00
|
|
|
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
|
2011-10-18 08:56:13 -07:00
|
|
|
}
|
2012-07-15 15:06:06 -07:00
|
|
|
} copy]);
|
2011-10-18 08:56:13 -07:00
|
|
|
}
|
|
|
|
|
2012-07-11 18:08:07 -07:00
|
|
|
+ (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;
|
|
|
|
}
|
|
|
|
|
2012-07-11 10:33:39 -07:00
|
|
|
@end
|