Pārlūkot izejas kodu

Adding profileImage property to bind image views to dynamically-loaded content

Mattt Thompson 13 gadi atpakaļ
vecāks
revīzija
ec2cee42d2

+ 0 - 18
Example/AFNetworking Mac Example.xcodeproj/project.pbxproj

@@ -119,9 +119,7 @@
 			children = (
 				F8129C311591073C009BFE23 /* AFTwitterAPIClient.h */,
 				F8129C251591073C009BFE23 /* AFTwitterAPIClient.m */,
-				F8129C261591073C009BFE23 /* Controllers */,
 				F8129C291591073C009BFE23 /* Models */,
-				F8129C2E1591073C009BFE23 /* Views */,
 				F8129C061591061B009BFE23 /* Supporting Files */,
 			);
 			name = Classes;
@@ -139,14 +137,6 @@
 			name = "Supporting Files";
 			sourceTree = "<group>";
 		};
-		F8129C261591073C009BFE23 /* Controllers */ = {
-			isa = PBXGroup;
-			children = (
-			);
-			name = Controllers;
-			path = Classes/Controllers;
-			sourceTree = SOURCE_ROOT;
-		};
 		F8129C291591073C009BFE23 /* Models */ = {
 			isa = PBXGroup;
 			children = (
@@ -159,14 +149,6 @@
 			path = Classes/Models;
 			sourceTree = SOURCE_ROOT;
 		};
-		F8129C2E1591073C009BFE23 /* Views */ = {
-			isa = PBXGroup;
-			children = (
-			);
-			name = Views;
-			path = Classes/Views;
-			sourceTree = SOURCE_ROOT;
-		};
 		F8129C4C15910901009BFE23 /* Vendor */ = {
 			isa = PBXGroup;
 			children = (

+ 6 - 2
Example/AppDelegate.m

@@ -33,8 +33,8 @@
 @synthesize navigationController = _navigationController;
 
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
-  NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:1024 * 1024 diskCapacity:1024 * 1024 * 5 diskPath:nil];
-[NSURLCache setSharedURLCache:URLCache];
+    NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:1024 * 1024 diskCapacity:1024 * 1024 * 5 diskPath:nil];
+    [NSURLCache setSharedURLCache:URLCache];
 
   [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
 
@@ -55,6 +55,7 @@
 #else
 
 #import "Tweet.h"
+#import "User.h"
 
 @implementation AppDelegate
 
@@ -62,6 +63,9 @@
 @synthesize tweetsArrayController = _tweetsArrayController;
 
 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
+    NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:1024 * 1024 diskCapacity:1024 * 1024 * 5 diskPath:nil];
+    [NSURLCache setSharedURLCache:URLCache];
+    
     [self.window makeKeyAndOrderFront:self];
     
     [Tweet publicTimelineTweetsWithBlock:^(NSArray *tweets) {

+ 4 - 0
Example/Classes/Models/User.h

@@ -30,4 +30,8 @@
 
 - (id)initWithAttributes:(NSDictionary *)attributes;
 
+#if __MAC_OS_X_VERSION_MIN_REQUIRED
+@property (nonatomic, strong) NSImage *profileImage;
+#endif
+
 @end

+ 41 - 2
Example/Classes/Models/User.m

@@ -21,12 +21,16 @@
 // THE SOFTWARE.
 
 #import "User.h"
+#import "AFImageRequestOperation.h"
+
+@interface User ()
++ (NSOperationQueue *)sharedProfileImageRequestOperationQueue;
+@end
 
 @implementation User {
 @private
-    NSUInteger _userID;
-    __strong NSString *_username;
     __strong NSString *_profileImageURLString;
+    __strong AFImageRequestOperation *_profileImageRequestOperation;
 }
 
 @synthesize userID = _userID;
@@ -49,4 +53,39 @@
     return [NSURL URLWithString:_profileImageURLString];
 }
 
+#if __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 && !_profileImageRequestOperation) {
+		_profileImageRequestOperation = [AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequest requestWithURL:self.profileImageURL] success:^(NSImage *image) {
+			self.profileImage = image;
+            
+			_profileImageRequestOperation = nil;
+		}];
+        
+		[_profileImageRequestOperation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {
+			return [[NSCachedURLResponse alloc] initWithResponse:cachedResponse.response data:cachedResponse.data userInfo:cachedResponse.userInfo storagePolicy:NSURLCacheStorageAllowed];
+		}];
+		
+        [[[self class] sharedProfileImageRequestOperationQueue] addOperation:_profileImageRequestOperation];
+	}
+	
+	return _profileImage;
+}
+
+#endif
+
 @end

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 564 - 148
Example/MainMenu.xib


+ 20 - 7
Example/main.m

@@ -1,10 +1,23 @@
-//
-//  main.m
-//  AFNetworking-Mac-Example
-//
-//  Created by Mattt Thompson on 12/06/19.
-//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
-//
+// main.m
+// Copyright (c) 2012 Mattt Thompson (http://mattt.me/)
+// 
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+// 
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
 
 #if __IPHONE_OS_VERSION_MIN_REQUIRED
   #import <UIKit/UIKit.h>

Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels