forked from thiagoperes/IDMPhotoBrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIDMPhoto.m
288 lines (234 loc) · 9.32 KB
/
IDMPhoto.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//
// IDMPhoto.m
// IDMPhotoBrowser
//
// Created by Michael Waterfall on 17/10/2010.
// Copyright 2010 d3i. All rights reserved.
//
#import "IDMPhoto.h"
#import "IDMPhotoBrowser.h"
// Private
@interface IDMPhoto () {
// Image Sources
NSString *_photoPath;
// Image
UIImage *_underlyingImage;
// Other
NSString *_caption;
BOOL _loadingInProgress;
}
// Properties
@property (nonatomic, strong) UIImage *underlyingImage;
// Methods
- (void)imageLoadingComplete;
@end
// IDMPhoto
@implementation IDMPhoto
// Properties
@synthesize underlyingImage = _underlyingImage,
photoURL = _photoURL,
caption = _caption;
#pragma mark Class Methods
+ (IDMPhoto *)photoWithImage:(UIImage *)image {
return [[IDMPhoto alloc] initWithImage:image];
}
+ (IDMPhoto *)photoWithFilePath:(NSString *)path {
return [[IDMPhoto alloc] initWithFilePath:path];
}
+ (IDMPhoto *)photoWithURL:(NSURL *)url {
return [[IDMPhoto alloc] initWithURL:url];
}
+ (NSArray *)photosWithImages:(NSArray *)imagesArray {
NSMutableArray *photos = [NSMutableArray arrayWithCapacity:imagesArray.count];
for (UIImage *image in imagesArray) {
if ([image isKindOfClass:[UIImage class]]) {
IDMPhoto *photo = [IDMPhoto photoWithImage:image];
[photos addObject:photo];
}
}
return photos;
}
+ (NSArray *)photosWithFilePaths:(NSArray *)pathsArray {
NSMutableArray *photos = [NSMutableArray arrayWithCapacity:pathsArray.count];
for (NSString *path in pathsArray) {
if ([path isKindOfClass:[NSString class]]) {
IDMPhoto *photo = [IDMPhoto photoWithFilePath:path];
[photos addObject:photo];
}
}
return photos;
}
+ (NSArray *)photosWithURLs:(NSArray *)urlsArray {
NSMutableArray *photos = [NSMutableArray arrayWithCapacity:urlsArray.count];
for (id url in urlsArray) {
if ([url isKindOfClass:[NSURL class]]) {
IDMPhoto *photo = [IDMPhoto photoWithURL:url];
[photos addObject:photo];
}
else if ([url isKindOfClass:[NSString class]]) {
IDMPhoto *photo = [IDMPhoto photoWithURL:[NSURL URLWithString:url]];
[photos addObject:photo];
}
}
return photos;
}
#pragma mark NSObject
- (id)initWithImage:(UIImage *)image {
if ((self = [super init])) {
self.underlyingImage = image;
}
return self;
}
- (id)initWithFilePath:(NSString *)path {
if ((self = [super init])) {
_photoPath = [path copy];
}
return self;
}
- (id)initWithURL:(NSURL *)url {
if ((self = [super init])) {
_photoURL = [url copy];
}
return self;
}
#pragma mark IDMPhoto Protocol Methods
- (UIImage *)underlyingImage {
return _underlyingImage;
}
- (void)loadUnderlyingImageAndNotify {
NSAssert([[NSThread currentThread] isMainThread], @"This method must be called on the main thread.");
_loadingInProgress = YES;
if (self.underlyingImage) {
// Image already loaded
[self imageLoadingComplete];
} else {
if (_photoPath) {
// Load async from file
[self performSelectorInBackground:@selector(loadImageFromFileAsync) withObject:nil];
} else if (_photoURL) {
// Load async from web (using SDWebImageManager)
[[SDWebImageManager sharedManager] loadImageWithURL:_photoURL options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
CGFloat progress = ((CGFloat)receivedSize)/((CGFloat)expectedSize);
if (self.progressUpdateBlock) {
self.progressUpdateBlock(progress);
}
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
if (image) {
self.underlyingImage = image;
}
[self performSelectorOnMainThread:@selector(imageLoadingComplete) withObject:nil waitUntilDone:NO];
}];
} else {
// Failed - no source
self.underlyingImage = nil;
[self imageLoadingComplete];
}
}
}
// Release if we can get it again from path or url
- (void)unloadUnderlyingImage {
_loadingInProgress = NO;
if (self.underlyingImage && (_photoPath || _photoURL)) {
self.underlyingImage = nil;
}
}
#pragma mark - Async Loading
/*- (UIImage *)decodedImageWithImage:(UIImage *)image {
CGImageRef imageRef = image.CGImage;
// System only supports RGB, set explicitly and prevent context error
// if the downloaded image is not the supported format
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL,
CGImageGetWidth(imageRef),
CGImageGetHeight(imageRef),
8,
// width * 4 will be enough because are in ARGB format, don't read from the image
CGImageGetWidth(imageRef) * 4,
colorSpace,
// kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little
// makes system don't need to do extra conversion when displayed.
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
CGColorSpaceRelease(colorSpace);
if ( ! context) {
return nil;
}
CGRect rect = (CGRect){CGPointZero, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)};
CGContextDrawImage(context, rect, imageRef);
CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage *decompressedImage = [[UIImage alloc] initWithCGImage:decompressedImageRef];
CGImageRelease(decompressedImageRef);
return decompressedImage;
}*/
- (UIImage *)decodedImageWithImage:(UIImage *)image {
if (image.images) {
// Do not decode animated images
return image;
}
CGImageRef imageRef = image.CGImage;
CGSize imageSize = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
CGRect imageRect = (CGRect){.origin = CGPointZero, .size = imageSize};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
int infoMask = (bitmapInfo & kCGBitmapAlphaInfoMask);
BOOL anyNonAlpha = (infoMask == kCGImageAlphaNone ||
infoMask == kCGImageAlphaNoneSkipFirst ||
infoMask == kCGImageAlphaNoneSkipLast);
// CGBitmapContextCreate doesn't support kCGImageAlphaNone with RGB.
// https://developer.apple.com/library/mac/#qa/qa1037/_index.html
if (infoMask == kCGImageAlphaNone && CGColorSpaceGetNumberOfComponents(colorSpace) > 1)
{
// Unset the old alpha info.
bitmapInfo &= ~kCGBitmapAlphaInfoMask;
// Set noneSkipFirst.
bitmapInfo |= kCGImageAlphaNoneSkipFirst;
}
// Some PNGs tell us they have alpha but only 3 components. Odd.
else if (!anyNonAlpha && CGColorSpaceGetNumberOfComponents(colorSpace) == 3)
{
// Unset the old alpha info.
bitmapInfo &= ~kCGBitmapAlphaInfoMask;
bitmapInfo |= kCGImageAlphaPremultipliedFirst;
}
// It calculates the bytes-per-row based on the bitsPerComponent and width arguments.
CGContextRef context = CGBitmapContextCreate(NULL,
imageSize.width,
imageSize.height,
CGImageGetBitsPerComponent(imageRef),
0,
colorSpace,
bitmapInfo);
CGColorSpaceRelease(colorSpace);
// If failed, return undecompressed image
if (!context) return image;
CGContextDrawImage(context, imageRect, imageRef);
CGImageRef decompressedImageRef = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage *decompressedImage = [UIImage imageWithCGImage:decompressedImageRef scale:image.scale orientation:image.imageOrientation];
CGImageRelease(decompressedImageRef);
return decompressedImage;
}
// Called in background
// Load image in background from local file
- (void)loadImageFromFileAsync {
@autoreleasepool {
@try {
self.underlyingImage = [UIImage imageWithContentsOfFile:_photoPath];
if (!_underlyingImage) {
//IDMLog(@"Error loading photo from path: %@", _photoPath);
}
} @finally {
self.underlyingImage = [self decodedImageWithImage: self.underlyingImage];
[self performSelectorOnMainThread:@selector(imageLoadingComplete) withObject:nil waitUntilDone:NO];
}
}
}
// Called on main
- (void)imageLoadingComplete {
NSAssert([[NSThread currentThread] isMainThread], @"This method must be called on the main thread.");
// Complete so notify
_loadingInProgress = NO;
[[NSNotificationCenter defaultCenter] postNotificationName:IDMPhoto_LOADING_DID_END_NOTIFICATION
object:self];
}
@end