This repository has been archived by the owner on May 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
RAResourceImageProvider.mm
90 lines (73 loc) · 2.78 KB
/
RAResourceImageProvider.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#import "RAResourceImageProvider.h"
#import "PDFImage.h"
const NSString *resourcePath = RA_BASE_PATH;
NSCache *_rsImgCache = [NSCache new];
@implementation RAResourceImageProvider
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize
{
// from: https://stackoverflow.com/questions/2658738/the-simplest-way-to-resize-an-uiimage
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
+(id) loadAndCacheImageWithStrippedPath:(NSString*)stripped
{
NSString *pdfPath = [NSString stringWithFormat:@"%@/Resources/%@.pdf",resourcePath,stripped];
NSString *pngPath = [NSString stringWithFormat:@"%@/Resources/%@.png",resourcePath,stripped];
if ([NSFileManager.defaultManager fileExistsAtPath:pdfPath])
{
RAPDFImage *pdf = [RAPDFImage imageWithContentsOfFile:pdfPath];
if (pdf)
[_rsImgCache setObject:pdf forKey:stripped];
return pdf;
}
else if ([NSFileManager.defaultManager fileExistsAtPath:pngPath])
{
UIImage *img = [UIImage imageWithContentsOfFile:pngPath];
if (img)
[_rsImgCache setObject:img forKey:stripped];
return img;
}
return nil;
}
+(id) getOrCacheImageWithFilename:(NSString*)strippedPath
{
return [_rsImgCache objectForKey:strippedPath] ?: [self loadAndCacheImageWithStrippedPath:strippedPath];
}
+(UIImage*) convertToUIImageIfNeeded:(id)arg sizeIfNeeded:(CGSize)size forceSizing:(BOOL)force
{
if ([arg isKindOfClass:UIImage.class])
{
if (force)
return [self imageWithImage:arg scaledToSize:size];
else
return (UIImage*)arg;
}
if ([arg isKindOfClass:[RAPDFImage class]])
{
UIImage *image = [arg imageWithOptions:[RAPDFImageOptions optionsWithSize:size]];
return image;
}
return nil;
}
+(UIImage*) imageForFilename:(NSString*)filename
{
NSString *strippedPath = [[filename lastPathComponent] stringByDeletingPathExtension];
id img = [self getOrCacheImageWithFilename:strippedPath];
return [self convertToUIImageIfNeeded:img sizeIfNeeded:CGSizeMake(200, 200) forceSizing:NO];
}
+(UIImage*) imageForFilename:(NSString*)filename size:(CGSize)size tintedTo:(UIColor*)tint
{
return [[self imageForFilename:filename constrainedToSize:size] _flatImageWithColor:tint];
}
+(UIImage*) imageForFilename:(NSString*)filename constrainedToSize:(CGSize)size
{
NSString *strippedPath = [[filename lastPathComponent] stringByDeletingPathExtension];
id img = [self getOrCacheImageWithFilename:strippedPath];
return [self convertToUIImageIfNeeded:img sizeIfNeeded:size forceSizing:YES];
}
@end