-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUIImage+Additions.m
284 lines (212 loc) · 7.53 KB
/
UIImage+Additions.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//
// UIImage+Additions.m
//
// Created by Wess Cope on 2/11/11.
// Copyright 2012. All rights reserved.
//
#import "UIImage+Additions.h"
@implementation UIImage(Additions)
+ (UIImage *)scaleImage:(UIImage *)incomingImage ToWidth:(CGFloat)width
{
UIImage *targetImage = nil;
CGFloat imageWidth = incomingImage.size.width;
CGFloat imageHeight = incomingImage.size.height;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
CGFloat newHeight = (width*imageHeight)/imageWidth;
CGSize newSize = CGSizeMake(width, newHeight);
UIGraphicsBeginImageContext(newSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = width;
thumbnailRect.size.height = newHeight;
[incomingImage drawInRect:thumbnailRect];
targetImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//if(targetImage == nil) NSLog(@"could not scale image");
return targetImage;
}
+ (UIImage *)scaleImage:(UIImage *)incomingImage ToHeight:(CGFloat)height
{
UIImage *targetImage = nil;
CGFloat imageWidth = incomingImage.size.width;
CGFloat imageHeight = incomingImage.size.height;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
CGFloat newWidth = (imageWidth*height)/imageHeight;
CGSize newSize = CGSizeMake(newWidth, height);
UIGraphicsBeginImageContext(newSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = newWidth;
thumbnailRect.size.height = height;
[incomingImage drawInRect:thumbnailRect];
targetImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//if(targetImage == nil) NSLog(@"could not scale image");
return targetImage;
}
+ (UIImage *)resizeImage:(UIImage *)image ToSize:(CGSize)size
{
UIImage *resultImage = nil;
UIGraphicsBeginImageContext(size);
CGRect targetFrame = CGRectZero;
targetFrame.size = size;
[image drawInRect:targetFrame];
resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}
+ (UIImage *)scaleToSize:(UIImage *)incomingImage targetSize:(CGSize)targetSize
{
UIImage *sourceImage = incomingImage;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
scaleFactor = (widthFactor < heightFactor)? widthFactor : heightFactor;
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
if (widthFactor < heightFactor)
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
else if (widthFactor > heightFactor)
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
// this is actually the interesting part:
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(newImage == nil) NSLog(@"could not scale image");
return newImage;
}
- (UIImage *)resizeTo:(CGSize)targetSize
{
return [UIImage resizeImage:self ToSize:targetSize];
}
+ (UIImage *)cropImage:(UIImage *)img toInset:(CGFloat)inset
{
CGRect imageRect = CGRectMake(0.0f, 0.0f, img.size.width, img.size.height);
CGRect newImgFrame = CGRectInset(imageRect, inset, inset);
CGImageRef cropped = CGImageCreateWithImageInRect(img.CGImage, newImgFrame);
UIImage *returnImage = [UIImage imageWithCGImage:cropped];
CGImageRelease(cropped);
return returnImage;
}
+ (UIImage *)cropImage:(UIImage *)img toSize:(CGSize)size
{
CGRect newImgFrame = CGRectZero;
newImgFrame.size = size;
CGImageRef cropped = CGImageCreateWithImageInRect(img.CGImage, newImgFrame);
UIImage *returnImage = [UIImage imageWithCGImage:cropped];
CGImageRelease(cropped);
return returnImage;
}
+ (UIImage *)cropImage:(UIImage *)img toSize:(CGSize)size atPoint:(CGPoint)point
{
CGRect newImgFrame = CGRectZero;
newImgFrame.size = size;
newImgFrame.origin = point;
CGImageRef cropped = CGImageCreateWithImageInRect(img.CGImage, newImgFrame);
UIImage *returnImage = [UIImage imageWithCGImage:cropped];
CGImageRelease(cropped);
return returnImage;
}
+ (UIImage *)ratioScale:(UIImage *)image toRect:(CGRect)frame fromSize:(CGSize)size
{
CGSize frameSize = frame.size;
CGSize imageSize = (CGSizeEqualToSize(size, CGSizeZero))? image.size : size;
CGFloat ratio = imageSize.width/imageSize.height;
CGFloat newWidth;
CGFloat newHeight;
CGFloat widthDifference;
CGFloat heightDifference;
if(imageSize.width > frameSize.width && imageSize.height > frameSize.height)
{
widthDifference = imageSize.width - frameSize.width;
if(imageSize.height > frameSize.height)
heightDifference = imageSize.height - frameSize.height;
if(heightDifference >= widthDifference)
{
newHeight = frameSize.height;
newWidth = newHeight*ratio;
}
else
{
newWidth = frameSize.width;
newHeight = newWidth/ratio;
}
}
else if(imageSize.width > frameSize.width)
{
newWidth = frameSize.width;
newHeight = newWidth/ratio;
}
else if(imageSize.height > frameSize.height)
{
newHeight = frameSize.height;
newWidth = newHeight*ratio;
}
else
{
newWidth = imageSize.width;
newHeight = imageSize.height;
}
return [UIImage resizeImage:image ToSize:CGSizeMake(newWidth, newHeight)];
}
+ (UIImage *)fillImage:(UIImage *)image withColor:(UIColor *)color
{
UIGraphicsBeginImageContextWithOptions([image size], NO, 0.0);
CGRect rect = CGRectZero;
rect.size = image.size;
[color set];
UIRectFill(rect);
[image drawInRect:rect blendMode:kCGBlendModeDestinationIn alpha:1.0];
UIImage *filledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return filledImage;
}
- (UIImage *)fillImageWithColor:(UIColor *)color
{
return [UIImage fillImage:self withColor:color];
}
+ (UIImage *)imageFromURL:(NSString *)urlString
{
UIImage *incoming = nil;
@try
{
incoming = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]];
}
@catch (NSException *exception)
{
}
return incoming;
}
+ (UIImage *)imageNamed:(NSString *)name withShadowColor:(UIColor *)color
{
UIImage *tmpImage = [UIImage imageNamed:name];
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef shadowContext = CGBitmapContextCreate(NULL, tmpImage.size.width + 10, tmpImage.size.height + 10, CGImageGetBitsPerComponent(tmpImage.CGImage), 0,
colourSpace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colourSpace);
CGContextSetShadowWithColor(shadowContext, CGSizeMake(5, -5), 5, color.CGColor);
CGContextDrawImage(shadowContext, CGRectMake(0, 10, tmpImage.size.width, tmpImage.size.height), tmpImage.CGImage);
CGImageRef shadowedCGImage = CGBitmapContextCreateImage(shadowContext);
CGContextRelease(shadowContext);
UIImage * shadowedImage = [UIImage imageWithCGImage:shadowedCGImage];
CGImageRelease(shadowedCGImage);
return shadowedImage;
}
@end