-
Notifications
You must be signed in to change notification settings - Fork 4
/
UTImage.m
110 lines (90 loc) · 3.7 KB
/
UTImage.m
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// UTImage.m
// RingFinder
//
// Created by Danny Morrow on 10/25/10.
// Copyright 2010 unitytheory.com. All rights reserved.
//
#import "UTImage.h"
@implementation UTImage
+ (UIImage *) imageWithContentsOfFile:(NSString *)path
{
NSString *parsedPath = [[NSBundle mainBundle] pathForResource:[path lastPathComponent] ofType:nil inDirectory:[path stringByDeletingLastPathComponent]];
return [UTImage imageWithContentsOfResolutionIndependentFile:parsedPath];
}
+ (UIImage *) imageWithFill:(UIColor *)color ofSize:(CGSize)aSize alpha:(float)alpha
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGRect r = CGRectMake(0, 0, aSize.width, aSize.height);
CGContextRef context = CGBitmapContextCreate (NULL, aSize.width, aSize.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
if (context==NULL) return NULL;
CGColorRef alphaColor = CGColorCreateCopyWithAlpha(color.CGColor, alpha);
CGContextSetFillColorWithColor(context, alphaColor);
CGColorRelease(alphaColor);
CGContextFillRect(context, r);
CGImageRef mainViewContentBitmapContext = CGBitmapContextCreateImage(context);
UIImage *theImage = [UIImage imageWithCGImage:mainViewContentBitmapContext];
CGImageRelease(mainViewContentBitmapContext);
CGContextRelease(context);
return theImage;
}
+ (NSString*) path:(NSString*)path atScale:(NSUInteger)scale
{
NSString* scaleString = [NSString stringWithFormat:@"@%@x", @(scale)];
if ([path rangeOfString:scaleString].location != NSNotFound) return path;
NSString* ipadLabel = @"";
if ([path rangeOfString:@"~ipad"].location != NSNotFound)
{
NSRange lastOccurance = [path rangeOfString:@"~ipad" options:NSBackwardsSearch];
ipadLabel = @"~ipad";
path = [path stringByReplacingCharactersInRange:lastOccurance withString:@""];
}
return [[path stringByDeletingLastPathComponent]
stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@%@.%@",
[[path lastPathComponent] stringByDeletingPathExtension],
scaleString,
ipadLabel,
[path pathExtension]]];
}
+ (NSString*) path2x:(NSString*)path
{
return [UTImage path:path atScale:2];
}
+ (NSString*) path3x:(NSString*)path
{
return [UTImage path:path atScale:3];
}
- (id)initWithContentsOfResolutionIndependentFile:(NSString *)path
{
CGFloat scale = [UIScreen mainScreen].scale;
for (NSUInteger i = 3; i>=2; i--)
{
if (scale >= i)
{
NSString *adjustedPath = [UTImage path:path atScale:i];
if ( [[NSFileManager defaultManager] fileExistsAtPath:adjustedPath] )
{
return [self initWithCGImage:[[UIImage imageWithData:[NSData dataWithContentsOfFile:adjustedPath]] CGImage] scale:i orientation:UIImageOrientationUp];
}
}
}
return [self initWithData:[NSData dataWithContentsOfFile:path]];
}
+ (UIImage*)imageWithContentsOfResolutionIndependentFile:(NSString *)path
{
return [[UTImage alloc] initWithContentsOfResolutionIndependentFile:path];
}
+ (UIImage *) circleWithRect:(CGRect)rect withFill:(UIColor*)fillColor
{
CGSize size = CGSizeMake(CGRectGetWidth(rect)+2*CGRectGetMinX(rect), CGRectGetHeight(rect)+2*CGRectGetMinY(rect));
UIGraphicsBeginImageContextWithOptions(size, FALSE, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(ctx, rect);
CGContextSetFillColorWithColor(ctx, fillColor.CGColor);
CGContextFillPath(ctx);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
@end