Skip to content

Commit

Permalink
Fixing image loading for the Mac OS X example app
Browse files Browse the repository at this point in the history
  • Loading branch information
mattt committed May 11, 2014
1 parent f62359b commit 154fee8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 41 deletions.
4 changes: 2 additions & 2 deletions Example/Classes/Models/User.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ extern NSString * const kUserProfileImageDidLoadNotification;
@property (readonly, nonatomic, copy) NSString *username;
@property (readonly, nonatomic, unsafe_unretained) NSURL *avatarImageURL;

- (instancetype)initWithAttributes:(NSDictionary *)attributes;

#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
@property (nonatomic, strong) NSImage *profileImage;
#endif

- (instancetype)initWithAttributes:(NSDictionary *)attributes;

@end
83 changes: 44 additions & 39 deletions Example/Classes/Models/User.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@
@interface User ()
@property (readwrite, nonatomic, assign) NSUInteger userID;
@property (readwrite, nonatomic, copy) NSString *username;
@property (readwrite, nonatomic, copy) NSString *avatarImageURLString;
@property (readwrite, nonatomic, strong) AFHTTPRequestOperation *avatarImageRequestOperation;

#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
+ (NSOperationQueue *)sharedProfileImageRequestOperationQueue;
@property (readwrite, nonatomic, copy) NSString *avatarImageURLString;
@property (readwrite, nonatomic, strong) AFHTTPRequestOperation *avatarImageRequestOperation;
#endif
@end

Expand All @@ -55,41 +54,47 @@ - (NSURL *)avatarImageURL {
return [NSURL URLWithString:self.avatarImageURLString];
}

//#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
//
//@synthesize profileImage = _profileImage;
//
//+ (NSOperationQueue *)sharedProfileImageRequestOperationQueue {
// static NSOperationQueue *_sharedProfileImageRequestOperationQueue = nil;
// static dispatch_once_t onceToken;
// dispatch_once(&onceToken, ^{
// _sharedProfileImageRequestOperationQueue = [[NSOperationQueue alloc] init];
// [_sharedProfileImageRequestOperationQueue setMaxConcurrentOperationCount:8];
// });
//
// return _sharedProfileImageRequestOperationQueue;
//}
//
//- (NSImage *)profileImage {
// if (!_profileImage && !_avatarImageRequestOperation) {
// _avatarImageRequestOperation = [AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequest requestWithURL:self.avatarImageURL] success:^(NSImage *image) {
// self.profileImage = image;
//
// _avatarImageRequestOperation = nil;
//
// [[NSNotificationCenter defaultCenter] postNotificationName:kUserProfileImageDidLoadNotification object:self userInfo:nil];
// }];
//
// [_avatarImageRequestOperation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {
// return [[NSCachedURLResponse alloc] initWithResponse:cachedResponse.response data:cachedResponse.data userInfo:cachedResponse.userInfo storagePolicy:NSURLCacheStorageAllowed];
// }];
//
// [[[self class] sharedProfileImageRequestOperationQueue] addOperation:_avatarImageRequestOperation];
// }
//
// return _profileImage;
//}
//
//#endif
#pragma mark -

#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED

+ (NSOperationQueue *)sharedProfileImageRequestOperationQueue {
static NSOperationQueue *_sharedProfileImageRequestOperationQueue = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedProfileImageRequestOperationQueue = [[NSOperationQueue alloc] init];
[_sharedProfileImageRequestOperationQueue setMaxConcurrentOperationCount:8];
});

return _sharedProfileImageRequestOperationQueue;
}

- (NSImage *)profileImage {
if (!_profileImage && !_avatarImageRequestOperation) {
NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:self.avatarImageURL];
[mutableRequest setValue:@"image/*" forHTTPHeaderField:@"Accept"];
AFHTTPRequestOperation *imageRequestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:mutableRequest];
imageRequestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[imageRequestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, NSImage *responseImage) {
self.profileImage = responseImage;

_avatarImageRequestOperation = nil;

[[NSNotificationCenter defaultCenter] postNotificationName:kUserProfileImageDidLoadNotification object:self userInfo:nil];
} failure:nil];

[imageRequestOperation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {
return [[NSCachedURLResponse alloc] initWithResponse:cachedResponse.response data:cachedResponse.data userInfo:cachedResponse.userInfo storagePolicy:NSURLCacheStorageAllowed];
}];

_avatarImageRequestOperation = imageRequestOperation;

[[[self class] sharedProfileImageRequestOperationQueue] addOperation:_avatarImageRequestOperation];
}

return _profileImage;
}

#endif

@end

0 comments on commit 154fee8

Please sign in to comment.