Browse Source

Fixing image loading for the Mac OS X example app

Mattt Thompson 11 years ago
parent
commit
154fee8afe
2 changed files with 46 additions and 41 deletions
  1. 2 2
      Example/Classes/Models/User.h
  2. 44 39
      Example/Classes/Models/User.m

+ 2 - 2
Example/Classes/Models/User.h

@@ -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

+ 44 - 39
Example/Classes/Models/User.m

@@ -28,11 +28,10 @@ NSString * const kUserProfileImageDidLoadNotification = @"com.alamofire.user.pro
 @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
 
@@ -55,41 +54,47 @@ NSString * const kUserProfileImageDidLoadNotification = @"com.alamofire.user.pro
     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