forked from ha-nyung/UIImage-Categories
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIImage+Resize.m
227 lines (191 loc) · 10.7 KB
/
UIImage+Resize.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
// UIImage+Resize.m
// Created by Trevor Harmon on 8/5/09.
// Free for personal or commercial use, with or without modification.
// No warranty is expressed or implied.
#import "UIImage+Resize.h"
#import "UIImage+RoundedCorner.h"
#import "UIImage+Alpha.h"
@implementation UIImage (Resize)
// Returns a copy of this image that is cropped to the given bounds.
// The bounds will be adjusted using CGRectIntegral.
- (UIImage *)croppedImage:(CGRect)bounds {
bounds = CGRectIntegral(CGRectMake(bounds.origin.x * self.scale, bounds.origin.y * self.scale,
bounds.size.width * self.scale, bounds.size.height * self.scale));
CGRect newBounds; // transformed bounds to preserve image orientation
switch ( self.imageOrientation ) {
case UIImageOrientationUpMirrored:
newBounds = CGRectMake(self.size.width - bounds.origin.x - bounds.size.width,
bounds.origin.y, bounds.size.width, bounds.size.height);
break;
case UIImageOrientationDown:
newBounds = CGRectMake(self.size.width - bounds.origin.x - bounds.size.width,
self.size.height - bounds.origin.y - bounds.size.height,
bounds.size.width, bounds.size.height);
break;
case UIImageOrientationDownMirrored:
newBounds = CGRectMake(bounds.origin.x, self.size.height - bounds.origin.y - bounds.size.height,
bounds.size.width, bounds.size.height);
break;
case UIImageOrientationRight:
newBounds = CGRectMake(bounds.origin.y, self.size.width - bounds.origin.x - bounds.size.width,
bounds.size.height, bounds.size.width);
break;
case UIImageOrientationRightMirrored:
newBounds = CGRectMake(self.size.height - bounds.origin.y - bounds.size.height,
self.size.width - bounds.origin.x - bounds.size.width,
bounds.size.height, bounds.size.width);
break;
case UIImageOrientationLeft:
newBounds = CGRectMake(self.size.height - bounds.origin.y - bounds.size.height, bounds.origin.x,
bounds.size.height, bounds.size.width);
break;
case UIImageOrientationLeftMirrored:
newBounds = CGRectMake(bounds.origin.y, bounds.origin.x, bounds.size.height, bounds.size.width);
break;
case UIImageOrientationUp:
default:
newBounds = bounds;
}
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], newBounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
CGImageRelease(imageRef);
return croppedImage;
}
// Returns a copy of this image that is squared to the thumbnail size.
// If transparentBorder is non-zero, a transparent border of the given size will be added around the edges of the thumbnail. (Adding a transparent border of at least one pixel in size has the side-effect of antialiasing the edges of the image when rotating it using Core Animation.)
- (UIImage *)thumbnailImage:(NSInteger)thumbnailSize
transparentBorder:(NSUInteger)borderSize
cornerRadius:(NSUInteger)cornerRadius
interpolationQuality:(CGInterpolationQuality)quality {
return [self thumbnailSize:CGSizeMake(thumbnailSize, thumbnailSize) transparentBorder:borderSize cornerRadius:cornerRadius interpolationQuality:quality];
}
- (UIImage *)thumbnailSize:(CGSize)thumbnailSize transparentBorder:(NSUInteger)borderSize cornerRadius:(NSUInteger)cornerRadius interpolationQuality:(CGInterpolationQuality)quality {
UIImage *resizedImage = [self resizedImageWithContentMode:UIViewContentModeScaleAspectFill
bounds:thumbnailSize
interpolationQuality:quality];
// Crop out any part of the image that's larger than the thumbnail size
// The cropped rect must be centered on the resized image
// Round the origin points so that the size isn't altered when CGRectIntegral is later invoked
CGRect cropRect = CGRectMake(round((resizedImage.size.width - thumbnailSize.width) / 2),
round((resizedImage.size.height - thumbnailSize.height) / 2),
thumbnailSize.width,
thumbnailSize.height);
UIImage *croppedImage = [resizedImage croppedImage:cropRect];
UIImage *transparentBorderImage = borderSize ? [croppedImage transparentBorderImage:borderSize] : croppedImage;
return [transparentBorderImage roundedCornerImage:cornerRadius borderSize:borderSize];
}
// Returns a rescaled copy of the image, taking into account its orientation
// The image will be scaled disproportionately if necessary to fit the bounds specified by the parameter
- (UIImage *)resizedImage:(CGSize)newSize interpolationQuality:(CGInterpolationQuality)quality {
BOOL drawTransposed;
CGAffineTransform transform = CGAffineTransformIdentity;
switch ( self.imageOrientation )
{
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
drawTransposed = YES;
break;
default:
drawTransposed = NO;
}
transform = [self transformForOrientation:newSize];
return [self resizedImage:newSize transform:transform drawTransposed:drawTransposed interpolationQuality:quality];
}
// Resizes the image according to the given content mode, taking into account the image's orientation
- (UIImage *)resizedImageWithContentMode:(UIViewContentMode)contentMode
bounds:(CGSize)bounds
interpolationQuality:(CGInterpolationQuality)quality {
CGFloat horizontalRatio = bounds.width / self.size.width;
CGFloat verticalRatio = bounds.height / self.size.height;
CGFloat ratio;
switch (contentMode) {
case UIViewContentModeScaleAspectFill:
ratio = MAX(horizontalRatio, verticalRatio);
break;
case UIViewContentModeScaleAspectFit:
ratio = MIN(horizontalRatio, verticalRatio);
break;
default:
[NSException raise:NSInvalidArgumentException format:@"Unsupported content mode: %d", contentMode];
}
CGSize newSize = CGSizeMake(self.size.width * ratio, self.size.height * ratio);
return [self resizedImage:newSize interpolationQuality:quality];
}
#pragma mark -
#pragma mark Private helper methods
// Returns a copy of the image that has been transformed using the given affine transform and scaled to the new size
// The new image's orientation will be UIImageOrientationUp, regardless of the current image's orientation
// If the new size is not integral, it will be rounded up
- (UIImage *)resizedImage:(CGSize)newSize
transform:(CGAffineTransform)transform
drawTransposed:(BOOL)transpose
interpolationQuality:(CGInterpolationQuality)quality {
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width * self.scale, newSize.height * self.scale));
CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width);
CGImageRef imageRef = self.CGImage;
// Fix for a colorspace / transparency issue that affects some types of
// images. See here: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/comment-page-2/#comment-39951
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmap =CGBitmapContextCreate( NULL,
newRect.size.width,
newRect.size.height,
8,
0,
colorSpace,
kCGImageAlphaPremultipliedLast );
CGColorSpaceRelease(colorSpace);
// Rotate and/or flip the image if required by its orientation
CGContextConcatCTM(bitmap, transform);
// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(bitmap, quality);
// Draw into the context; this scales the image
CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef);
// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:self.scale orientation:UIImageOrientationUp];
// Clean up
CGContextRelease(bitmap);
CGImageRelease(newImageRef);
return newImage;
}
// Returns an affine transform that takes into account the image orientation when drawing a scaled image
- (CGAffineTransform)transformForOrientation:(CGSize)newSize {
CGAffineTransform transform = CGAffineTransformIdentity;
switch (self.imageOrientation) {
case UIImageOrientationDown: // EXIF = 3
case UIImageOrientationDownMirrored: // EXIF = 4
transform = CGAffineTransformTranslate(transform, newSize.width, newSize.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationLeft: // EXIF = 6
case UIImageOrientationLeftMirrored: // EXIF = 5
transform = CGAffineTransformTranslate(transform, newSize.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
case UIImageOrientationRight: // EXIF = 8
case UIImageOrientationRightMirrored: // EXIF = 7
transform = CGAffineTransformTranslate(transform, 0, newSize.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
default:
break;
}
switch (self.imageOrientation) {
case UIImageOrientationUpMirrored: // EXIF = 2
case UIImageOrientationDownMirrored: // EXIF = 4
transform = CGAffineTransformTranslate(transform, newSize.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationLeftMirrored: // EXIF = 5
case UIImageOrientationRightMirrored: // EXIF = 7
transform = CGAffineTransformTranslate(transform, newSize.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
default:
break;
}
return transform;
}
@end