Permalink
Browse files

Adding profileImage property to bind image views to dynamically-loade…

…d content
  • Loading branch information...
mattt committed Jun 19, 2012
1 parent a3f3971 commit ec2cee42d20368f86cb542bd33985554578205f9
@@ -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 = (
View
@@ -33,8 +33,8 @@ @implementation AppDelegate
@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,13 +55,17 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
#else
#import "Tweet.h"
+#import "User.h"
@implementation AppDelegate
@synthesize window = _window;
@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) {
@@ -30,4 +30,8 @@
- (id)initWithAttributes:(NSDictionary *)attributes;
+#if __MAC_OS_X_VERSION_MIN_REQUIRED
+@property (nonatomic, strong) NSImage *profileImage;
+#endif
+
@end
@@ -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 @@ - (NSURL *)profileImageURL {
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
Oops, something went wrong.

0 comments on commit ec2cee4

Please sign in to comment.