forked from udevsharold/dockx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIImage+Resize.m
89 lines (65 loc) · 3.59 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
#import "common.h"
#import "UIImage+Resize.h"
@implementation UIImage (Resize)
+(UIImage *)imageWithImage:(UIImage *)image resize:(CGSize)newSize{
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:newSize];
UIImage *newImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext*_Nonnull myContext) {
[image drawInRect:(CGRect) {.origin = CGPointZero, .size = newSize}];
}];
return [newImage imageWithRenderingMode:image.renderingMode];
}
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize inRect:(CGRect)rect{
//Determine whether the screen is retina
if ([[UIScreen mainScreen] scale] == 2.0) {
UIGraphicsBeginImageContextWithOptions(newSize, YES, 2.0);
}else{
UIGraphicsBeginImageContext(newSize);
}
//Draw image in provided rect
[image drawInRect:rect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//Pop this context
UIGraphicsEndImageContext();
return newImage;
}
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize{
return [UIImage imageWithImage:image scaledToSize:newSize inRect:(CGRect) {.origin = CGPointZero, .size = newSize}];
}
+ (UIImage *)imageWithImage:(UIImage *)image scaledToFitToSize:(CGSize)newSize{
//Only scale images down
if (image.size.width < newSize.width && image.size.height < newSize.height){
return [image copy];
}
//Determine the scale factors
CGFloat widthScale = newSize.width/image.size.width;
CGFloat heightScale = newSize.height/image.size.height;
CGFloat scaleFactor;
//The smaller scale factor will scale more (0 < scaleFactor < 1) leaving the other dimension inside the newSize rect
widthScale < heightScale ? (scaleFactor = widthScale) : (scaleFactor = heightScale);
CGSize scaledSize = CGSizeMake(image.size.width * scaleFactor, image.size.height * scaleFactor);
//Scale the image
return [UIImage imageWithImage:image scaledToSize:scaledSize inRect:CGRectMake(0.0, 0.0, scaledSize.width, scaledSize.height)];
}
+ (UIImage *)imageWithImage:(UIImage *)image scaledToFillToSize:(CGSize)newSize{
//Only scale images down
if (image.size.width < newSize.width && image.size.height < newSize.height) {
return [image copy];
}
//Determine the scale factors
CGFloat widthScale = newSize.width/image.size.width;
CGFloat heightScale = newSize.height/image.size.height;
CGFloat scaleFactor;
//The larger scale factor will scale less (0 < scaleFactor < 1) leaving the other dimension hanging outside the newSize rect
widthScale > heightScale ? (scaleFactor = widthScale) : (scaleFactor = heightScale);
CGSize scaledSize = CGSizeMake(image.size.width * scaleFactor, image.size.height * scaleFactor);
//Create origin point so that the center of the image falls into the drawing context rect (the origin will have negative component).
CGPoint imageDrawOrigin = CGPointMake(0, 0);
widthScale > heightScale ? (imageDrawOrigin.y = (newSize.height - scaledSize.height) * 0.5) :
(imageDrawOrigin.x = (newSize.width - scaledSize.width) * 0.5);
//Create rect where the image will draw
CGRect imageDrawRect = CGRectMake(imageDrawOrigin.x, imageDrawOrigin.y, scaledSize.width, scaledSize.height);
//The imageDrawRect is larger than the newSize rect, where the imageDraw origin is located defines what part of
//the image will fall into the newSize rect.
return [UIImage imageWithImage:image scaledToSize:newSize inRect:imageDrawRect];
}
@end