-
Notifications
You must be signed in to change notification settings - Fork 19
/
DBFBProfilePictureView.m
executable file
·619 lines (502 loc) · 21.9 KB
/
DBFBProfilePictureView.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
* Copyright 2012
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "DBFBProfilePictureView.h"
#import <AFNetworking/AFNetworking.h>
#include <CommonCrypto/CommonDigest.h>
@interface DBFBProfilePictureRequestPrivate : NSObject
//#define USE_AFNETWORKING_2
#ifdef USE_AFNETWORKING_2
@property (strong,nonatomic) AFHTTPRequestOperation* requestOperation;
#else
@property (strong,nonatomic) AFImageRequestOperation* requestOperation;
#endif
@property (strong,nonatomic) NSMutableSet* requestorsToUpdate;
@end
@implementation DBFBProfilePictureRequestPrivate
- (id)initWithProfilePictureView:(DBFBProfilePictureView*)view
{
self = [super init];
if(self) {
_requestorsToUpdate = [NSMutableSet setWithObject:view];
}
return self;
}
@end
@interface DBFBProfilePictureCachePrivate : NSObject
@property (nonatomic, readonly) UIImage* imageObject;
@property (nonatomic, strong) NSDate *lastUsed;
@end
@implementation DBFBProfilePictureCachePrivate
{
__strong UIImage *_strongImage;
__weak UIImage *_weakImage;
}
- (id)initWithImage:(UIImage*)image weakCaching:(BOOL)weak
{
self = [super init];
if(self) {
if (weak) {
_weakImage = image;
} else {
_strongImage = image;
}
_lastUsed = [NSDate date];
}
return self;
}
- (UIImage *)imageObject
{
if (_weakImage != nil) {
return _weakImage;
} else {
return _strongImage;
}
}
@end
@interface DBFBProfilePictureView()
@property (readonly, nonatomic) NSDictionary *imageQueryParam;
@property (strong, nonatomic) NSDictionary *previousImageQueryParam;
@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) NSURL *url;
@end
@implementation DBFBProfilePictureView
#pragma mark - Lifecycle
- (id)init {
self = [super init];
if (self) {
[self initialize];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initialize];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self initialize];
}
return self;
}
- (void)dealloc {
[self removeObserver:self forKeyPath:@"profileID"];
[self removeObserver:self forKeyPath:@"pictureCropping"];
[self removeObserver:self forKeyPath:@"showEmptyImage"];
[self removeObserver:self forKeyPath:@"emptyImage"];
}
#pragma mark -
- (NSDictionary *)imageQueryParam {
static CGFloat screenScaleFactor = 0.0;
if (screenScaleFactor == 0.0) {
screenScaleFactor = [[UIScreen mainScreen] scale];
}
// Retina display doesn't increase the bounds that iOS returns. The larger size to fetch needs
// to be calculated using the scale factor accessed above.
int width = (int)(self.bounds.size.width * screenScaleFactor);
if (self.pictureCropping == FBSDKProfilePictureModeSquare) {
if(width == 0) {
return nil;
}
return @{
@"width" : [NSString stringWithFormat:@"%d",width],
@"height" : [NSString stringWithFormat:@"%d",width],
};
}
// For non-square images, we choose between three variants knowing that the small profile picture is
// 50 pixels wide, normal is 100, and large is about 200.
if (width <= 50) {
return @{@"type":@"small"};
} else if (width <= 100) {
return @{@"type":@"normal"};
} else {
return @{@"type":@"large"};
}
}
- (void)initialize {
// the base class can cause virtual recursion, so
// to handle this we make initialize idempotent
if (self.imageView) {
return;
}
UIImageView* imageView = [[UIImageView alloc] initWithFrame:self.bounds];
imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.imageView = imageView;
self.autoresizesSubviews = YES;
self.clipsToBounds = YES;
[self addSubview:self.imageView];
[self addObserver:self forKeyPath:@"profileID" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"pictureCropping" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"showEmptyImage" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"emptyImage" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqualToString:@"profileID"] || [keyPath isEqualToString:@"pictureCropping"] || [keyPath isEqualToString:@"showEmptyImage"] || [keyPath isEqualToString:@"emptyImage"]) {
[self refreshImage:YES];
}
}
+ (NSOperationQueue *)sharedProfileImageRequestOperationQueue
{
static NSOperationQueue *_profileImageRequestOperationQueue = nil;
static dispatch_once_t done;
dispatch_once(&done, ^{
_profileImageRequestOperationQueue = [[NSOperationQueue alloc] init];
[_profileImageRequestOperationQueue setMaxConcurrentOperationCount:2];
});
return _profileImageRequestOperationQueue;
}
+ (NSMutableDictionary *)sharedCacheDictionary
{
static NSMutableDictionary *_sharedCacheDictionary = nil;
static dispatch_once_t done;
dispatch_once(&done, ^{
_sharedCacheDictionary = [[NSMutableDictionary alloc] init];
});
return _sharedCacheDictionary;
}
static NSInteger _maxImagesCachedBeyondLifetime = 0;
static NSTimeInterval _diskCacheLifetime = 0;
static BOOL _diskCacheEnabled = NO;
+ (void)enableDiskCache:(BOOL)enable lifetime:(NSTimeInterval)lifetime
{
_diskCacheEnabled = enable;
_diskCacheLifetime = lifetime;
}
+ (BOOL)shouldCacheImageBeyondLifetime
{
return _maxImagesCachedBeyondLifetime > 0;
}
+ (NSInteger)maxImagesCachedBeyondLifetime
{
return _maxImagesCachedBeyondLifetime;
}
+ (void)setMaxImagesCachedBeyondLifetime:(NSInteger)maxImagesCachedBeyondLifetime
{
NSAssert(maxImagesCachedBeyondLifetime >= 0, @"Error: maxImagesCachedBeyondLifetime must be zero or greater");
if (maxImagesCachedBeyondLifetime != _maxImagesCachedBeyondLifetime) {
if (_maxImagesCachedBeyondLifetime == 0 || maxImagesCachedBeyondLifetime == 0) {
// clear the cache if we have changed from weak to strong caching
NSMutableDictionary *cache = [self.class sharedCacheDictionary];
@synchronized(cache) {
[cache removeAllObjects];
}
}
_maxImagesCachedBeyondLifetime = maxImagesCachedBeyondLifetime;
}
}
static BOOL cleanupScheduled = NO;
- (void)cleanCache
{
BOOL needCleanup = NO;
NSMutableDictionary *cache = [self.class sharedCacheDictionary];
@synchronized(cache) {
if(!cleanupScheduled) {
needCleanup = YES;
cleanupScheduled = YES;
}
}
if(needCleanup) {
//clean up the cache some time later (30 seconds), it only depends on class methods
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 30ull * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
@synchronized(cache) {
cleanupScheduled = NO;
BOOL cacheBeyondLifetime = [[self class] shouldCacheImageBeyondLifetime];
if (!cacheBeyondLifetime) {
for (NSString *key in cache.allKeys) {
DBFBProfilePictureCachePrivate *cachedItem = cache[key];
if(cachedItem.imageObject == nil) {
[cache removeObjectForKey:key];
}
}
} else {
NSInteger maxImagesCached = [[self class] maxImagesCachedBeyondLifetime];
//go through cache and delete oldest items if we have too many of them
if (cache.count > maxImagesCached) {
//sort cache key by oldest items first
NSArray *sortedKeys = [cache keysSortedByValueUsingComparator:^NSComparisonResult(DBFBProfilePictureCachePrivate *obj1, DBFBProfilePictureCachePrivate *obj2) {
return [obj1.lastUsed compare:obj2.lastUsed];
}];
NSMutableArray *sortedKeysTruncated = sortedKeys.mutableCopy;
[sortedKeysTruncated removeObjectsInRange:NSMakeRange(cache.count - maxImagesCached, maxImagesCached)];
[cache removeObjectsForKeys:sortedKeysTruncated];
}
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *contents = [fileManager contentsOfDirectoryAtURL:[NSURL fileURLWithPath:[self diskCacheRoot]]
includingPropertiesForKeys:@[]
options:NSDirectoryEnumerationSkipsHiddenFiles
error:nil];
NSDate *today = [NSDate date];
for (NSURL *fileURL in contents) {
NSDate *expirationDate = nil;
NSDictionary *attributes = [fileManager attributesOfItemAtPath:[fileURL path] error:nil];
expirationDate = [attributes[NSFileCreationDate] dateByAddingTimeInterval:_diskCacheLifetime];
if ([expirationDate compare:today] == NSOrderedAscending) {
[fileManager removeItemAtURL:fileURL error:nil];
}
}
}
});
}
}
- (void)cacheImage:(UIImage*)image forURL:(NSURL*)url
{
[self cleanCache];
BOOL cacheBeyondLifetime = [[self class] shouldCacheImageBeyondLifetime];
DBFBProfilePictureCachePrivate *cachedItem = [[DBFBProfilePictureCachePrivate alloc] initWithImage:image weakCaching:!cacheBeyondLifetime];
NSMutableDictionary *cache = [self.class sharedCacheDictionary];
@synchronized(cache) {
[cache setObject:cachedItem forKey:url];
if (_diskCacheEnabled) {
[UIImagePNGRepresentation(image) writeToFile:[self filePathForCachedImage:url] atomically:YES];
}
}
}
- (NSString *)diskCacheRoot
{
NSString *cacheDir = @"DBFBProfilePictureViewCache";
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
return [documentsPath stringByAppendingPathComponent:cacheDir];
}
- (NSString *)getMD5FromString:(NSString *)source
{
const char *src = [source UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(src, (CC_LONG)strlen(src), result);
NSString *ret = [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
return [ret lowercaseString];
}
- (NSString *)filePathForCachedImage:(NSURL *)url
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *imagesPath = [self diskCacheRoot];
if (![fileManager fileExistsAtPath:imagesPath]) {
[fileManager createDirectoryAtPath:imagesPath withIntermediateDirectories:YES attributes:nil error:nil];
}
return [NSString stringWithFormat:@"%@/%@.png", imagesPath,[self getMD5FromString:[url absoluteString]]];
}
- (BOOL)localyCachedImageExists:(NSURL *)url
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [self filePathForCachedImage:url];
return [fileManager fileExistsAtPath:filePath];
}
- (UIImage*)cachedImageForURL:(NSURL*)url
{
[self cleanCache];
if (_diskCacheEnabled && [self localyCachedImageExists:url]) {
NSData *restoredData = [NSData dataWithContentsOfFile:[self filePathForCachedImage:url]];
if (restoredData != nil) {
return [UIImage imageWithData:restoredData];
}
}
NSMutableDictionary *cache = [self.class sharedCacheDictionary];
@synchronized(cache) {
UIImage* cachedImage = nil;
DBFBProfilePictureCachePrivate* cachedItem = [cache objectForKey:url];
if(cachedItem != nil) {
cachedItem.lastUsed = [NSDate date];
cachedImage = cachedItem.imageObject;
if(cachedImage == nil) {
[cache removeObjectForKey:url];
}
}
return cachedImage;
}
}
+ (NSMutableDictionary *)sharedImageRequestDictionary
{
static NSMutableDictionary *_sharedImageRequestDictionary = nil;
static dispatch_once_t done;
dispatch_once(&done, ^{
_sharedImageRequestDictionary = [[NSMutableDictionary alloc] init];
});
return _sharedImageRequestDictionary;
}
- (void)imageDownloadComplete:(UIImage*)image forURL:(NSURL*)url
{
NSMutableDictionary* requestsInProgress = [[self class] sharedImageRequestDictionary];
DBFBProfilePictureRequestPrivate *pictureRequest = nil;
@synchronized(requestsInProgress) {
pictureRequest = [requestsInProgress objectForKey:url];
[requestsInProgress removeObjectForKey:url];
}
[self cacheImage:image forURL:url];
for(DBFBProfilePictureView* pictureView in pictureRequest.requestorsToUpdate) {
pictureView.imageView.image = image;
[pictureView ensureImageViewContentMode];
if(pictureView.completionHandler != nil) {
pictureView.completionHandler(pictureView, nil);
}
}
}
- (void)removeFromRequestorsList
{
if (self.url) {
NSMutableDictionary* requestsInProgress = [[self class] sharedImageRequestDictionary];
DBFBProfilePictureRequestPrivate *pictureRequest = nil;
@synchronized(requestsInProgress) {
pictureRequest = [requestsInProgress objectForKey:self.url];
[pictureRequest.requestorsToUpdate removeObject:self];
if (pictureRequest.requestorsToUpdate.count == 0) {
[requestsInProgress removeObjectForKey:self.url];
[pictureRequest.requestOperation cancel];
}
}
self.url = nil;
}
}
- (void)imageDownloadFailedForURL:(NSURL*)url withError:(NSError*)error
{
NSMutableDictionary* requestsInProgress = [[self class] sharedImageRequestDictionary];
DBFBProfilePictureRequestPrivate *pictureRequest = nil;
@synchronized(requestsInProgress) {
pictureRequest = [requestsInProgress objectForKey:url];
[requestsInProgress removeObjectForKey:url];
}
for(DBFBProfilePictureView* pictureView in pictureRequest.requestorsToUpdate) {
if(pictureView.completionHandler != nil) {
pictureView.completionHandler(self, error);
}
}
}
- (void)requestImageDownload:(NSURL*)url
{
self.url = url;
NSMutableDictionary* requestsInProgress = [[self class] sharedImageRequestDictionary];
DBFBProfilePictureRequestPrivate *pictureRequest = nil;
@synchronized(requestsInProgress) {
pictureRequest = [requestsInProgress objectForKey:url];
if(pictureRequest != nil) {
[pictureRequest.requestorsToUpdate addObject:self];
pictureRequest = nil;
} else {
pictureRequest = [[DBFBProfilePictureRequestPrivate alloc] initWithProfilePictureView:self];
[requestsInProgress setObject:pictureRequest forKey:url];
}
}
if(pictureRequest) {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[request setHTTPShouldHandleCookies:NO];
[request setHTTPShouldUsePipelining:YES];
#ifdef USE_AFNETWORKING_2
AFHTTPRequestOperation* requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[self imageDownloadComplete:responseObject forURL:url];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self imageDownloadFailedForURL:url withError:error];
}];
#else
AFImageRequestOperation* requestOperation = [AFImageRequestOperation imageRequestOperationWithRequest:request
imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
[self imageDownloadComplete:image forURL:url];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError* error){
[self imageDownloadFailedForURL:url withError:error];
}];
#endif
[[[self class] sharedProfileImageRequestOperationQueue] addOperation:requestOperation];
pictureRequest.requestOperation = requestOperation;
}
if(self.startHandler != nil) {
self.startHandler(self);
}
}
- (void)refreshImage:(BOOL)forceRefresh
{
NSDictionary *newImageQueryParam = self.imageQueryParam;
// If not forcing refresh, check to see if the previous size we used would be the same
// as what we'd request now, as this method could be called often on control bounds animation,
// and we only want to fetch when needed.
if (!forceRefresh && [self.previousImageQueryParam isEqualToDictionary:newImageQueryParam]) {
// But we still may need to adjust the contentMode.
[self ensureImageViewContentMode];
return;
}
self.imageView.image = nil;
BOOL showEmptyImage = self.showEmptyImage;
if (self.profileID && newImageQueryParam) {
[self removeFromRequestorsList];
// Create the request to let the Facebook SDK handle the URL
NSString *graphPath = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture",self.profileID];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:graphPath]];
showEmptyImage = NO;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// Get the url
NSURL *url = response.URL;
UIImage* cachedImage = [self cachedImageForURL:url];
if(cachedImage != nil) {
self.imageView.image = cachedImage;
[self ensureImageViewContentMode];
if(self.completionHandler != nil)
{
self.completionHandler(self, nil);
}
}
else
{
[self requestImageDownload:url];
}
}];
}
if(showEmptyImage) {
if (self.emptyImage == nil) {
_emptyImage = [UIImage new];
}
self.imageView.image = self.emptyImage;
[self ensureImageViewContentMode];
}
self.previousImageQueryParam = newImageQueryParam;
}
- (void)ensureImageViewContentMode {
// Setting aspect fit allways should be fine
//
// // Set the image's contentMode such that if the image is larger than the control, we scale it down, preserving aspect
// // ratio. Otherwise, we center it. This ensures that we never scale up, and pixellate, the image.
// CGSize viewSize = self.bounds.size;
// CGSize imageSize = self.imageView.image.size;
// UIViewContentMode contentMode;
//
// // If both of the view dimensions are larger than the image, we'll center the image to prevent scaling up.
// // Note that unlike in choosing the image size, we *don't* use any Retina-display scaling factor to choose centering
// // vs. filling. If we were to do so, we'd get profile pics shrinking to fill the the view on non-Retina, but getting
// // centered and clipped on Retina.
// if (viewSize.width > imageSize.width && viewSize.height > imageSize.height) {
// contentMode = UIViewContentModeCenter;
// } else {
// contentMode = UIViewContentModeScaleAspectFit;
// }
// self.imageView.contentMode = contentMode;
self.contentMode = UIViewContentModeScaleAspectFit;
}
// Lets us catch resizes of the control, or any outer layout, allowing us to potentially
// choose a different image.
- (void)layoutSubviews {
[self refreshImage:NO];
[super layoutSubviews];
}
@end