-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIImage+Luban_iOS_Extension_h.m
386 lines (316 loc) · 14.5 KB
/
UIImage+Luban_iOS_Extension_h.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//
// UIImage+Luban_iOS_Extension_h.m
// Luban-iOS
//
// Created by guo on 2017/7/20.
// Copyright © 2017年 guo. All rights reserved.
//
#import <objc/runtime.h>
#import "UIImage+Luban_iOS_Extension_h.h"
@implementation UIImage (Luban_iOS_Extension_h)
static char isCustomImage;
static char customImageName;
+ (NSData *)luban_compressImage:(UIImage *)image withMask:(NSString *)maskName {
double size;
NSData *datalen = UIImageJPEGRepresentation(image, 1);
NSLog(@"Luban-iOS image data size before compressed == %f Kb",datalen.length/1024.0);
int fixelW = (int)image.size.width;
int fixelH = (int)image.size.height;
int thumbW = fixelW % 2 == 1 ? fixelW + 1 : fixelW;
int thumbH = fixelH % 2 == 1 ? fixelH + 1 : fixelH;
double scale = ((double)fixelW/fixelH);
if (scale <= 1 && scale > 0.5625) {
if (fixelH < 1664) {
if (datalen.length/1024.0 < 150) {
return datalen;
}
size = (fixelW * fixelH) / pow(1664, 2) * 150;
size = size < 60 ? 60 : size;
}
else if (fixelH >= 1664 && fixelH < 4990) {
thumbW = fixelW / 2;
thumbH = fixelH / 2;
size = (thumbH * thumbW) / pow(2495, 2) * 300;
size = size < 60 ? 60 : size;
}
else if (fixelH >= 4990 && fixelH < 10240) {
thumbW = fixelW / 4;
thumbH = fixelH / 4;
size = (thumbW * thumbH) / pow(2560, 2) * 300;
size = size < 100 ? 100 : size;
}
else {
int multiple = fixelH / 1280 == 0 ? 1 : fixelH / 1280;
thumbW = fixelW / multiple;
thumbH = fixelH / multiple;
size = (thumbW * thumbH) / pow(2560, 2) * 300;
size = size < 100 ? 100 : size;
}
}
else if (scale <= 0.5625 && scale > 0.5) {
if (fixelH < 1280 && datalen.length/1024 < 200) {
return datalen;
}
int multiple = fixelH / 1280 == 0 ? 1 : fixelH / 1280;
thumbW = fixelW / multiple;
thumbH = fixelH / multiple;
size = (thumbW * thumbH) / (1440.0 * 2560.0) * 400;
size = size < 100 ? 100 : size;
}
else {
int multiple = (int)ceil(fixelH / (1280.0 / scale));
thumbW = fixelW / multiple;
thumbH = fixelH / multiple;
size = ((thumbW * thumbH) / (1280.0 * (1280 / scale))) * 500;
size = size < 100 ? 100 : size;
}
return [self luban_compressWithImage:image thumbW:thumbW thumbH:thumbH size:size withMask:maskName];
}
+ (NSData *)luban_compressWithImage:(UIImage *)image thumbW:(int)width thumbH:(int)height size:(double)size withMask:(NSString *)maskName {
UIImage *thumbImage = [image fixOrientation];
thumbImage = [thumbImage resizeImage:image thumbWidth:width thumbHeight:height withMask:maskName];
int qualityCompress = 1.0;
NSData *dataLen = UIImageJPEGRepresentation(thumbImage, qualityCompress);
NSUInteger lenght = dataLen.length;
while (lenght / 1024 > size && qualityCompress > 0.06) {
qualityCompress -= 0.06;
dataLen = UIImageJPEGRepresentation(thumbImage, qualityCompress);
lenght = dataLen.length;
thumbImage = [UIImage imageWithData:dataLen];
}
NSLog(@"Luban-iOS image data size after compressed ==%f kb",dataLen.length/1024.0);
return dataLen;
}
#pragma mark -
+ (UIImage *)lubanCompressImage:(UIImage *)image {
return [self lubanCompressImage:image withMask:nil];
}
+ (UIImage *)lubanCompressImage:(UIImage *)image withMask:(NSString *)maskName {
double size;
NSData *datalen = UIImageJPEGRepresentation(image, 1);
NSLog(@"Luban-iOS image data size before compressed == %f Kb",datalen.length/1024.0);
int fixelW = (int)image.size.width;
int fixelH = (int)image.size.height;
int thumbW = fixelW % 2 == 1 ? fixelW + 1 : fixelW;
int thumbH = fixelH % 2 == 1 ? fixelH + 1 : fixelH;
double scale = ((double)fixelW/fixelH);
if (scale <= 1 && scale > 0.5625) {
if (fixelH < 1664) {
if (datalen.length/1024.0 < 150) {
return image;
}
size = (fixelW * fixelH) / pow(1664, 2) * 150;
size = size < 60 ? 60 : size;
}
else if (fixelH >= 1664 && fixelH < 4990) {
thumbW = fixelW / 2;
thumbH = fixelH / 2;
size = (thumbH * thumbW) / pow(2495, 2) * 300;
size = size < 60 ? 60 : size;
}
else if (fixelH >= 4990 && fixelH < 10240) {
thumbW = fixelW / 4;
thumbH = fixelH / 4;
size = (thumbW * thumbH) / pow(2560, 2) * 300;
size = size < 100 ? 100 : size;
}
else {
int multiple = fixelH / 1280 == 0 ? 1 : fixelH / 1280;
thumbW = fixelW / multiple;
thumbH = fixelH / multiple;
size = (thumbW * thumbH) / pow(2560, 2) * 300;
size = size < 100 ? 100 : size;
}
}
else if (scale <= 0.5625 && scale > 0.5) {
if (fixelH < 1280 && datalen.length/1024 < 200) {
return image;
}
int multiple = fixelH / 1280 == 0 ? 1 : fixelH / 1280;
thumbW = fixelW / multiple;
thumbH = fixelH / multiple;
size = (thumbW * thumbH) / (1440.0 * 2560.0) * 400;
size = size < 100 ? 100 : size;
}
else {
int multiple = (int)ceil(fixelH / (1280.0 / scale));
thumbW = fixelW / multiple;
thumbH = fixelH / multiple;
size = ((thumbW * thumbH) / (1280.0 * (1280 / scale))) * 500;
size = size < 100 ? 100 : size;
}
return [self compressWithImage:image thumbW:thumbW thumbH:thumbH size:size withMask:maskName];
}
+ (UIImage *)lubanCompressImage:(UIImage *)image withCustomImage:(NSString *)imageName {
if (imageName) {
objc_setAssociatedObject(self, &isCustomImage, @(1), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
objc_setAssociatedObject(self, &customImageName, imageName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return [self lubanCompressImage:image withMask:nil];
}
+ (UIImage *)compressWithImage:(UIImage *)image thumbW:(int)width thumbH:(int)height size:(double)size withMask:(NSString *)maskName {
UIImage *thumbImage = [image fixOrientation];
thumbImage = [thumbImage resizeImage:image thumbWidth:width thumbHeight:height withMask:maskName];
int qualityCompress = 1.0;
NSData *dataLen = UIImageJPEGRepresentation(thumbImage, qualityCompress);
NSUInteger lenght = dataLen.length;
while (lenght / 1024 > size && qualityCompress > 0.06) {
qualityCompress -= 0.06;
dataLen = UIImageJPEGRepresentation(thumbImage, qualityCompress);
lenght = dataLen.length;
thumbImage = [UIImage imageWithData:dataLen];
}
NSLog(@"Luban-iOS image data size after compressed ==%f kb",dataLen.length/1024.0);
return thumbImage;
}
// specify the size
- (UIImage *)resizeImage:(UIImage *)image thumbWidth:(int)width thumbHeight:(int)height withMask:(NSString *)maskName {
int outW = (int)image.size.width;
int outH = (int)image.size.height;
int inSampleSize = 1;
if (outW > width || outH > height) {
int halfW = outW / 2;
int halfH = outH / 2;
while ((halfH / inSampleSize) > height && (halfW / inSampleSize) > width) {
inSampleSize *= 2;
}
}
int heightRatio = (int)ceil(outH / (float) height);
int widthRatio = (int)ceil(outW / (float) width);
if (heightRatio > 1 || widthRatio > 1) {
inSampleSize = heightRatio > widthRatio ? heightRatio : widthRatio;
}
CGSize thumbSize = CGSizeMake((NSUInteger)((CGFloat)outW/widthRatio), (NSUInteger)((CGFloat)outH/heightRatio));
UIGraphicsBeginImageContext(thumbSize);
CGContextRef context = UIGraphicsGetCurrentContext();
[image drawInRect:CGRectMake(0, 0, thumbSize.width, thumbSize.height)];
if (maskName) {
CGContextTranslateCTM (context, thumbSize.width / 2, thumbSize.height / 2);
CGContextScaleCTM (context, 1, -1);
[self drawMaskWithString:maskName context:context radius:0 angle:0 colour:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5] font:[UIFont systemFontOfSize:38.0] slantAngle:(CGFloat)(M_PI/6) size:thumbSize];
}
else {
BOOL iscustom = objc_getAssociatedObject(self, &isCustomImage);
if (iscustom) {
NSString *imageName = objc_getAssociatedObject(self, &customImageName);
UIImage *imageMask = [UIImage imageNamed:imageName];
if (imageMask) {
[imageMask drawInRect:CGRectMake(0, 0, thumbSize.width, thumbSize.height)];
}
}
}
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
objc_setAssociatedObject(self, &isCustomImage, @(NO), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
return resultImage;
}
- (UIImage *)fixOrientation {
// No-op if the orientation is already correct
if (self.imageOrientation == UIImageOrientationUp) return self;
// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
CGAffineTransform transform = CGAffineTransformIdentity;
switch (self.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, 0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, self.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
case UIImageOrientationUp:
case UIImageOrientationUpMirrored:
break;
}
switch (self.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, self.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, self.size.height, 0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
case UIImageOrientationUp:
case UIImageOrientationDown:
case UIImageOrientationLeft:
case UIImageOrientationRight:
break;
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
CGImageGetBitsPerComponent(self.CGImage), 0,
CGImageGetColorSpace(self.CGImage),
CGImageGetBitmapInfo(self.CGImage));
CGContextConcatCTM(ctx, transform);
switch (self.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
break;
default:
CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
break;
}
// And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}
- (void) drawMaskWithString:(NSString *)str context:(CGContextRef)context radius:(CGFloat)radius angle:(CGFloat)angle colour:(UIColor *)colour font:(UIFont *)font slantAngle:(CGFloat)slantAngle size:(CGSize)size{
// *******************************************************
// This draws the String str centred at the position
// specified by the polar coordinates (r, theta)
// i.e. the x= r * cos(theta) y= r * sin(theta)
// and rotated by the angle slantAngle
// *******************************************************
// Set the text attributes
NSDictionary *attributes = @{NSForegroundColorAttributeName:colour,
NSFontAttributeName:font};
// Save the context
CGContextSaveGState(context);
// Undo the inversion of the Y-axis (or the text goes backwards!)
CGContextScaleCTM(context, 1, -1);
// Move the origin to the centre of the text (negating the y-axis manually)
CGContextTranslateCTM(context, radius * cos(angle), -(radius * sin(angle)));
// Rotate the coordinate system
CGContextRotateCTM(context, -slantAngle);
// Calculate the width of the text
CGSize offset = [str sizeWithAttributes:attributes];
// Move the origin by half the size of the text
CGContextTranslateCTM (context, -offset.width / 2, -offset.height / 2); // Move the origin to the centre of the text (negating the y-axis manually)
// Draw the text
NSInteger width = ceil(cos(slantAngle)*offset.width);
NSInteger height = ceil(sin(slantAngle)*offset.width);
NSInteger row = size.height/(height+100.0);
NSInteger coloum = size.width/(width+100.0)>6?:6;
CGFloat xPoint = 0;
CGFloat yPoint = 0;
for (NSInteger index = 0; index < row*coloum; index++) {
xPoint = (index%coloum) *(width+100.0)-[UIScreen mainScreen].bounds.size.width;
yPoint = (index/coloum) *(height+100.0);
[str drawAtPoint:CGPointMake(xPoint, yPoint) withAttributes:attributes];
xPoint += -[UIScreen mainScreen].bounds.size.width;
yPoint += -[UIScreen mainScreen].bounds.size.height;
[str drawAtPoint:CGPointMake(xPoint, yPoint) withAttributes:attributes];
}
// Restore the context
CGContextRestoreGState(context);
}
@end