Repository navigation
Expand file tree
/
Copy pathWDImageData.m
More file actions
177 lines (138 loc) · 4.43 KB
/
Copy pathWDImageData.m
File metadata and controls
177 lines (138 loc) · 4.43 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
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
//
// WDImageData.m
// Inkpad
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2010-2013 Steve Sprang
//
#import "UIImage+Additions.h"
#import "WDImageData.h"
#import "WDUtilities.h"
NSString *WDDataKey = @"WDDataKey";
#define kThumbnailDimension 128
@implementation WDImageData
@synthesize data = imageData_;
@synthesize naturalBounds = naturalBounds_;
@synthesize image = image_;
@synthesize thumbnailImage = thumbnailImage_;
+ (WDImageData *) imageDataWithData:(NSData *)data
{
return [[WDImageData alloc] initWithData:data];
}
- (id) initWithData:(NSData *)data
{
self = [super init];
if (!self) {
return nil;
}
imageData_ = data;
#if TARGET_OS_IPHONE
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedMemoryWarning:)
name:UIApplicationDidReceiveMemoryWarningNotification
object:[UIApplication sharedApplication]];
#endif
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:imageData_ forKey:WDDataKey];
}
- (id)initWithCoder:(NSCoder *)coder
{
self = [super init];
imageData_ = [coder decodeObjectForKey:WDDataKey];
#if TARGET_OS_IPHONE
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedMemoryWarning:)
name:UIApplicationDidReceiveMemoryWarningNotification
object:[UIApplication sharedApplication]];
#endif
return self;
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void) receivedMemoryWarning:(NSNotification *)aNotification
{
if (receivedMemoryWarning_) {
// we've already received the warning and discarded the large cache
return;
}
receivedMemoryWarning_ = YES;
image_ = nil;
}
- (NSString *) mimetype
{
if (self.imageFormat == WDImageDataJPEGFormat) {
return @"image/jpeg";
} else {
return @"image/png";
}
}
- (WDImageDataFormat) imageFormat
{
UInt8 buffer[4];
[imageData_ getBytes:buffer length:4];
if (buffer[0] == 0xFF && buffer[1] == 0xD8 && buffer[2] == 0xFF && buffer[3] == 0xE0) {
return WDImageDataJPEGFormat;
}
return WDImageDataPNGFormat;
}
- (UIImage *) inflatedImage
{
UIImage *compressed = [[UIImage alloc] initWithData:imageData_];
size_t width = compressed.size.width;
size_t height = compressed.size.height;
CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
size_t bitmapBytesPerRow = (width * 4);
colorSpace = CGColorSpaceCreateDeviceRGB();
context = CGBitmapContextCreate (NULL, width, height, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), compressed.CGImage);
CGImageRef result = CGBitmapContextCreateImage(context);
CFRelease(context);
UIImage *inflated = [UIImage imageWithCGImage:result];
CGImageRelease(result);
return inflated;
}
- (UIImage *) image
{
if (!image_) {
if (!receivedMemoryWarning_) {
// keep the uncompressed image in memory... this increases rendering speed but uses a lot more memory
// if we ever get a memory warning we'll switch to the compressed image
image_ = [self inflatedImage];
} else {
// use the compressed image
image_ = [[UIImage alloc] initWithData:imageData_];
}
naturalBounds_ = CGRectMake(0, 0, image_.size.width, image_.size.height);
// use this image when rendering thumbnails
if (!thumbnailImage_) {
// only need to build this once
thumbnailImage_ = [image_ downsampleWithMaxDimension:kThumbnailDimension];
}
}
return image_;
}
- (UIImage *) thumbnailImage
{
if (!thumbnailImage_) {
[self image];
}
return thumbnailImage_;
}
- (NSData *) digest
{
return WDSHA1DigestForData(imageData_);
}
- (id) copyWithZone:(NSZone *)zone
{
return self;
}
@end