NewsBlur/media/ios/Classes/Utilities.m

131 lines
4.5 KiB
Mathematica
Raw Normal View History

//
// Utilities.m
// NewsBlur
//
// Created by Samuel Clay on 10/17/11.
// Copyright (c) 2011 NewsBlur. All rights reserved.
//
#import "Utilities.h"
void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor,
CGColorRef endColor) {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = { 0.0, 1.0 };
NSArray *colors = [NSArray arrayWithObjects:(__bridge id)startColor, (__bridge id)endColor, nil];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace,
(__bridge CFArrayRef) colors, locations);
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
CGContextSaveGState(context);
CGContextAddRect(context, rect);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
@implementation Utilities
static NSMutableDictionary *imageCache;
+ (void)saveImage:(UIImage *)image feedId:(NSString *)filename {
if (!imageCache) {
2012-07-15 15:06:06 -07:00
imageCache = [NSMutableDictionary dictionary];
}
// Save image to memory-based cache, for performance when reading.
2012-08-09 12:31:09 -07:00
// NSLog(@"Saving %@", [imageCache allKeys]);
if (image) {
[imageCache setObject:image forKey:filename];
} else {
2012-11-08 17:39:32 -08:00
// 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 {
2012-08-13 18:45:06 -07:00
if (isSocial) {
2012-08-13 17:35:04 -07:00
// return [UIImage imageNamed:@"user_light.png"];
2012-08-13 18:45:06 -07:00
return nil;
} else {
return [UIImage imageNamed:@"world.png"];
}
}
}
2012-07-22 09:12:02 -07:00
+ (void)drawLinearGradientWithRect:(CGRect)rect startColor:(CGColorRef)startColor endColor:(CGColorRef)endColor {
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = { 0.0, 1.0 };
NSArray *colors = [NSArray arrayWithObjects:(__bridge id)startColor, (__bridge id)endColor, nil];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace,
(__bridge CFArrayRef) colors, locations);
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
CGContextSaveGState(context);
CGContextAddRect(context, rect);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
+ (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]) {
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];
}
2012-07-15 15:06:06 -07:00
} copy]);
}
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;
}
@end