-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIImage_AutoResizing.m
More file actions
122 lines (80 loc) · 3.78 KB
/
UIImage_AutoResizing.m
File metadata and controls
122 lines (80 loc) · 3.78 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
//
// UIImage_AutoResizing.m
// AutoResizingImages
//
// Created by Zachry Thayer on 12/4/11.
// Copyright (c) 2011 Zachry Thayer. All rights reserved.
//
#import "UIImage_AutoResizing.h"
unsigned int colorFromDataAtPoint(unsigned int* data, unsigned int dataStride,CGPoint point);
unsigned int colorFromDataAtPoint(unsigned int* data, unsigned int dataStride,CGPoint point)
{
unsigned int offset = (dataStride * point.y) + point.x;
return data[offset];
}
@implementation UIImage (AutoResizing)
+ (UIImage*)autoResizingImageNamed:(NSString*)imageName{
UIImage *original = [UIImage imageNamed:imageName];
CGFloat scale = [original scale];
UIImage *returnImage = original;
CGImageRef imageRef = [original CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned int *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast |
kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
// Snag the first pixel color
unsigned int firstPixel = colorFromDataAtPoint(rawData, width, CGPointMake(0, 0));
// Key color is found run logic this is a scaling image
if (firstPixel == UIImage_AutoResizingKeyColor) {
// First rerender the image to remove extra width and height
UIGraphicsBeginImageContext(CGSizeMake(width - scale, height - scale));
[original drawInRect:CGRectMake(-scale, -scale, width, height)];
returnImage = UIGraphicsGetImageFromCurrentImageContext();
returnImage = [UIImage imageWithCGImage:[returnImage CGImage] scale:scale orientation:UIImageOrientationUp];
UIGraphicsEndImageContext();
original = nil;
//if you don't use ARC
//[original release];
// Second Determine the UIEdgeInsets
UIEdgeInsets edgeInsets = UIEdgeInsetsZero;
for (int x = 0; x < width; x ++) {
unsigned int color = colorFromDataAtPoint(rawData, width, CGPointMake(x*scale, 0));
if (color == UIImage_AutoResizingMarkerColor) {//first cap found
if (edgeInsets.left == 0) {
edgeInsets.left = x;
}
else
{
edgeInsets.right = width - x;
}
}
}
for (int y = 0; y < height; y ++) {
unsigned int color = colorFromDataAtPoint(rawData, width, CGPointMake(0,y*scale));
if (color == UIImage_AutoResizingMarkerColor) {//first cap found
if (edgeInsets.top == 0) {
edgeInsets.top = y;
}
else
{
edgeInsets.bottom = height - y;
}
}
}
returnImage = [returnImage resizableImageWithCapInsets:edgeInsets];
}
// Cleanup
free(rawData);
return returnImage;
}
@end