-
Notifications
You must be signed in to change notification settings - Fork 10
/
UIImage+Utils.m
137 lines (97 loc) · 4.15 KB
/
UIImage+Utils.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
//
// UIImage+Utils.m
//
// Created by Lucas Eduardo on 18/05/15.
// Copyright (c) 2015 wemob. All rights reserved.
//
#import "UIImage+Utils.h"
@implementation UIImage (Utils)
-(UIImage*)cropImageInRect:(CGRect)cropRect forParentBound:(CGRect)parentBounds;
{
CGRect rect = [self cropRectForFrame:cropRect inBounds:parentBounds];
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
// stick to methods on UIImage so that orientation etc. are automatically
// dealt with for us
[self drawAtPoint:CGPointMake(-rect.origin.x, -rect.origin.y)];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
-(UIImage*)imageWithRoundedBounds
{
CGSize size = self.size;
CGFloat radius = MIN(size.width, size.height) / 2.0;
return [self imageWithCornerRadius:radius];
}
-(UIImage*)imageWithCornerRadius:(CGFloat)cornerRadius
{
return [self imageWithCornerInset:UICornerInsetMake(cornerRadius, cornerRadius, cornerRadius, cornerRadius)];
}
#pragma mark - private methods
-(CGRect)cropRectForFrame:(CGRect)frame inBounds:(CGRect)parentBounds
{
CGFloat widthScale = parentBounds.size.width / self.size.width;
CGFloat heightScale = parentBounds.size.height / self.size.height;
float x, y, w, h, offset;
if (widthScale<heightScale) {
offset = (parentBounds.size.height - (self.size.height*widthScale))/2;
x = frame.origin.x / widthScale;
y = (frame.origin.y-offset) / widthScale;
w = frame.size.width / widthScale;
h = frame.size.height / widthScale;
} else {
offset = (parentBounds.size.width - (self.size.width*heightScale))/2;
x = (frame.origin.x-offset) / heightScale;
y = frame.origin.y / heightScale;
w = frame.size.width / heightScale;
h = frame.size.height / heightScale;
}
return CGRectMake(x, y, w, h);
}
- (UIImage *)imageWithCornerInset:(UICornerInset)cornerInset
{
if (![self isValidCornerInset:cornerInset])
return nil;
CGFloat scale = self.scale;
CGRect rect = CGRectMake(0.0f, 0.0f, scale*self.size.width, scale*self.size.height);
cornerInset.topRight *= scale;
cornerInset.topLeft *= scale;
cornerInset.bottomLeft *= scale;
cornerInset.bottomRight *= scale;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, rect.size.width, rect.size.height, 8, 0, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
if (context == NULL)
return nil;
CGFloat minx = CGRectGetMinX(rect), midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect);
CGFloat miny = CGRectGetMinY(rect), midy = CGRectGetMidY(rect), maxy = CGRectGetMaxY(rect);
CGContextBeginPath(context);
CGContextMoveToPoint(context, minx, midy);
CGContextAddArcToPoint(context, minx, miny, midx, miny, cornerInset.bottomLeft);
CGContextAddArcToPoint(context, maxx, miny, maxx, midy, cornerInset.bottomRight);
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, cornerInset.topRight);
CGContextAddArcToPoint(context, minx, maxy, minx, midy, cornerInset.topLeft);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawImage(context, rect, self.CGImage);
CGImageRef bitmapImageRef = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage *newImage = [UIImage imageWithCGImage:bitmapImageRef scale:scale orientation:UIImageOrientationUp];
CGImageRelease(bitmapImageRef);
return newImage;
}
- (BOOL)isValidCornerInset:(UICornerInset)cornerInset
{
CGSize size = self.size;
BOOL isValid = YES;
if (cornerInset.topLeft + cornerInset.topRight > size.width)
isValid = NO;
else if (cornerInset.topRight + cornerInset.bottomRight > size.height)
isValid = NO;
else if (cornerInset.bottomRight + cornerInset.bottomLeft > size.width)
isValid = NO;
else if (cornerInset.bottomLeft + cornerInset.topLeft > size.height)
isValid = NO;
return isValid;
}
@end