-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathUIImage+ResizeMagick.m
169 lines (141 loc) · 6.94 KB
/
UIImage+ResizeMagick.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
//
// UIImage+ResizeMagick.m
//
//
// Created by Vlad Andersen on 1/5/13.
//
//
#import "UIImage+ResizeMagick.h"
static CGInterpolationQuality _interpolationQuality = kCGInterpolationNone;
@implementation UIImage (ResizeMagick)
// width Width given, height automagically selected to preserve aspect ratio.
// xheight Height given, width automagically selected to preserve aspect ratio.
// widthxheight Maximum values of height and width given, aspect ratio preserved.
// widthxheight^ Minimum values of width and height given, aspect ratio preserved.
// widthxheight! Exact dimensions, no aspect ratio preserved.
// widthxheight# Crop to this exact dimensions.
+(void)setInterpolationQuality:(CGInterpolationQuality)quality {
_interpolationQuality = quality;
}
+(CGInterpolationQuality)interpolationQuality {
return _interpolationQuality;
}
- (UIImage *) resizedImageByMagick: (NSString *) spec
{
if([spec hasSuffix:@"!"]) {
NSString *specWithoutSuffix = [spec substringToIndex: [spec length] - 1];
NSArray *widthAndHeight = [specWithoutSuffix componentsSeparatedByString: @"x"];
NSUInteger width = labs([[widthAndHeight objectAtIndex: 0] integerValue]);
NSUInteger height = labs([[widthAndHeight objectAtIndex: 1] integerValue]);
UIImage *newImage = [self resizedImageWithMinimumSize: CGSizeMake (width, height)];
return [newImage drawImageInBounds: CGRectMake (0, 0, width, height)];
}
if([spec hasSuffix:@"#"]) {
NSString *specWithoutSuffix = [spec substringToIndex: [spec length] - 1];
NSArray *widthAndHeight = [specWithoutSuffix componentsSeparatedByString: @"x"];
NSUInteger width = labs([[widthAndHeight objectAtIndex: 0] integerValue]);
NSUInteger height = labs([[widthAndHeight objectAtIndex: 1] integerValue]);
UIImage *newImage = [self resizedImageWithMinimumSize: CGSizeMake (width, height)];
return [newImage croppedImageWithRect: CGRectMake ((newImage.size.width - width) / 2, (newImage.size.height - height) / 2, width, height)];
}
if([spec hasSuffix:@"^"]) {
NSString *specWithoutSuffix = [spec substringToIndex: [spec length] - 1];
NSArray *widthAndHeight = [specWithoutSuffix componentsSeparatedByString: @"x"];
return [self resizedImageWithMinimumSize: CGSizeMake (labs([[widthAndHeight objectAtIndex: 0] integerValue]),
labs([[widthAndHeight objectAtIndex: 1] integerValue]))];
}
NSArray *widthAndHeight = [spec componentsSeparatedByString: @"x"];
if ([widthAndHeight count] == 1) {
return [self resizedImageByWidth: [spec integerValue]];
}
if ([[widthAndHeight objectAtIndex: 0] isEqualToString: @""]) {
return [self resizedImageByHeight: labs([[widthAndHeight objectAtIndex: 1] integerValue])];
}
return [self resizedImageWithMaximumSize: CGSizeMake (labs([[widthAndHeight objectAtIndex: 0] integerValue]),
labs([[widthAndHeight objectAtIndex: 1] integerValue]))];
}
- (CGImageRef) CGImageWithCorrectOrientation CF_RETURNS_RETAINED
{
if (self.imageOrientation == UIImageOrientationDown) {
//retaining because caller expects to own the reference
CGImageRef cgImage = [self CGImage];
CGImageRetain(cgImage);
return cgImage;
}
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, _interpolationQuality);
if (self.imageOrientation == UIImageOrientationRight) {
CGContextRotateCTM (context, 90 * M_PI/180);
} else if (self.imageOrientation == UIImageOrientationLeft) {
CGContextRotateCTM (context, -90 * M_PI/180);
} else if (self.imageOrientation == UIImageOrientationUp) {
CGContextRotateCTM (context, 180 * M_PI/180);
}
[self drawAtPoint:CGPointMake(0, 0)];
CGImageRef cgImage = CGBitmapContextCreateImage(context);
UIGraphicsEndImageContext();
return cgImage;
}
- (UIImage *) resizedImageByWidth: (NSUInteger) width
{
CGImageRef imgRef = [self CGImageWithCorrectOrientation];
CGFloat original_width = CGImageGetWidth(imgRef);
CGFloat original_height = CGImageGetHeight(imgRef);
CGFloat ratio = width/original_width;
CGImageRelease(imgRef);
return [self drawImageInBounds: CGRectMake(0, 0, width, round(original_height * ratio))];
}
- (UIImage *) resizedImageByHeight: (NSUInteger) height
{
CGImageRef imgRef = [self CGImageWithCorrectOrientation];
CGFloat original_width = CGImageGetWidth(imgRef);
CGFloat original_height = CGImageGetHeight(imgRef);
CGFloat ratio = height/original_height;
CGImageRelease(imgRef);
return [self drawImageInBounds: CGRectMake(0, 0, round(original_width * ratio), height)];
}
- (UIImage *) resizedImageWithMinimumSize: (CGSize) size
{
CGImageRef imgRef = [self CGImageWithCorrectOrientation];
CGFloat original_width = CGImageGetWidth(imgRef);
CGFloat original_height = CGImageGetHeight(imgRef);
CGFloat width_ratio = size.width / original_width;
CGFloat height_ratio = size.height / original_height;
CGFloat scale_ratio = width_ratio > height_ratio ? width_ratio : height_ratio;
CGImageRelease(imgRef);
return [self drawImageInBounds: CGRectMake(0, 0, round(original_width * scale_ratio), round(original_height * scale_ratio))];
}
- (UIImage *) resizedImageWithMaximumSize: (CGSize) size
{
CGImageRef imgRef = [self CGImageWithCorrectOrientation];
CGFloat original_width = CGImageGetWidth(imgRef);
CGFloat original_height = CGImageGetHeight(imgRef);
CGFloat width_ratio = size.width / original_width;
CGFloat height_ratio = size.height / original_height;
CGFloat scale_ratio = width_ratio < height_ratio ? width_ratio : height_ratio;
CGImageRelease(imgRef);
return [self drawImageInBounds: CGRectMake(0, 0, round(original_width * scale_ratio), round(original_height * scale_ratio))];
}
- (UIImage *) drawImageInBounds: (CGRect) bounds
{
UIGraphicsBeginImageContextWithOptions(bounds.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, _interpolationQuality);
[self drawInRect: bounds];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resizedImage;
}
- (UIImage*) croppedImageWithRect: (CGRect) rect {
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, _interpolationQuality);
CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, self.size.width, self.size.height);
CGContextClipToRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));
[self drawInRect:drawRect];
UIImage* subImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return subImage;
}
@end