forked from dmorrow/ObjectiveC-Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UTImage.m
93 lines (74 loc) · 3.21 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
//
// 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*) path2x:(NSString*)path
{
if ([path rangeOfString:@"@2x"].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:@"%@@2x%@.%@",
[[path lastPathComponent] stringByDeletingPathExtension],
ipadLabel,
[path pathExtension]]];
}
- (id)initWithContentsOfResolutionIndependentFile:(NSString *)path
{
if ( [[[UIDevice currentDevice] systemVersion] intValue] >= 4 && [[UIScreen mainScreen] scale] == 2.0 ) {
NSString *path2x = [UTImage path2x:path];
if ( [[NSFileManager defaultManager] fileExistsAtPath:path2x] ) {
return [self initWithCGImage:[[UIImage imageWithData:[NSData dataWithContentsOfFile:path2x]] CGImage] scale:2.0 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(rect.size.width+2*rect.origin.x, rect.size.height+2*rect.origin.y);
UIGraphicsBeginImageContextWithOptions(size, FALSE, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(ctx, rect);
CGContextSetFillColorWithColor(ctx, fillColor.CGColor);
CGContextFillPath(ctx);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
@end