-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathUIImage+Border.m
More file actions
123 lines (100 loc) · 4.67 KB
/
Copy pathUIImage+Border.m
File metadata and controls
123 lines (100 loc) · 4.67 KB
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
//
// UIImage+Border.m
//
// Created by Levey on 11/10/11.
// Copyright (c) 2011 Vanillatech. All rights reserved.
//
#import "UIImage+Border.h"
static CGImageRef CreateMask(CGSize size, NSUInteger thickness)
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapContext = CGBitmapContextCreate(NULL,
size.width,
size.height,
8,
size.width * 32,
colorSpace,
kCGBitmapByteOrderDefault | kCGImageAlphaNone);
if (bitmapContext == NULL)
{
NSLog(@"create mask bitmap context failed");
return nil;
}
// fill the black color in whole size, anything in black area will be transparent.
CGContextSetFillColorWithColor(bitmapContext, [UIColor blackColor].CGColor);
CGContextFillRect(bitmapContext, CGRectMake(0, 0, size.width, size.height));
// fill the white color in whole size, anything in white area will keep.
CGContextSetFillColorWithColor(bitmapContext, [UIColor whiteColor].CGColor);
CGContextFillRect(bitmapContext, CGRectMake(thickness, thickness, size.width - thickness * 2, size.height - thickness * 2));
// acquire the mask
CGImageRef maskImageRef = CGBitmapContextCreateImage(bitmapContext);
// clean up
CGContextRelease(bitmapContext);
CGColorSpaceRelease(colorSpace);
return maskImageRef;
}
@implementation UIImage(Border)
- (UIImage *)imageWithColoredBorder:(NSUInteger)borderThickness borderColor:(UIColor *)color withShadow:(BOOL)withShadow
{
size_t shadowThickness = 0;
if (withShadow)
{
shadowThickness = 2;
}
size_t newWidth = self.size.width + 2 * borderThickness + 2 * shadowThickness;
size_t newHeight = self.size.height + 2 * borderThickness + 2 * shadowThickness;
CGRect imageRect = CGRectMake(borderThickness + shadowThickness, borderThickness + shadowThickness, self.size.width, self.size.height);
UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight));
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextSetShadow(ctx, CGSizeZero, 4.5f);
[color setFill];
CGContextFillRect(ctx, CGRectMake(shadowThickness, shadowThickness, newWidth - 2 * shadowThickness, newHeight - 2 * shadowThickness));
CGContextRestoreGState(ctx);
[self drawInRect:imageRect];
//CGContextDrawImage(ctx, imageRect, self.CGImage); //if use this method, image will be filp vertically
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
return img;
}
- (UIImage *)imageWithTransparentBorder:(NSUInteger)thickness
{
size_t newWidth = self.size.width + 2 * thickness;
size_t newHeight = self.size.height + 2 * thickness;
size_t bitsPerComponent = 8;
size_t bitsPerPixel = 32;
size_t bytesPerRow = bitsPerPixel * newWidth;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
if(colorSpace == NULL)
{
NSLog(@"create color space failed");
return nil;
}
CGContextRef bitmapContext = CGBitmapContextCreate(NULL,
newWidth,
newHeight,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast);
if (bitmapContext == NULL)
{
NSLog(@"create bitmap context failed");
return nil;
}
// acquire image with opaque border
CGRect imageRect = CGRectMake(thickness, thickness, self.size.width, self.size.height);
CGContextDrawImage(bitmapContext, imageRect, self.CGImage);
CGImageRef opaqueBorderImageRef = CGBitmapContextCreateImage(bitmapContext);
// acquire image with transparent border
CGImageRef maskImageRef = CreateMask(CGSizeMake(newWidth, newHeight), thickness);
CGImageRef transparentBorderImageRef = CGImageCreateWithMask(opaqueBorderImageRef, maskImageRef);
UIImage *transparentBorderImage = [UIImage imageWithCGImage:transparentBorderImageRef];
// clean up
CGColorSpaceRelease(colorSpace);
CGContextRelease(bitmapContext);
CGImageRelease(opaqueBorderImageRef);
CGImageRelease(maskImageRef);
CGImageRelease(transparentBorderImageRef);
return transparentBorderImage;
}
@end