diff options
author | Vitaly Takmazov | 2013-11-05 22:25:33 +0400 |
---|---|---|
committer | Vitaly Takmazov | 2013-11-05 22:25:33 +0400 |
commit | c3750f7891ab73ed4c4bfa8ddc59969fe77b0d9e (patch) | |
tree | 583f068ea0da76667b3d96b45f6c1e8fc5d1f44e /Juick | |
parent | a795a089148f73389f1e35430ed94a9aec9156e7 (diff) |
load avatars async
Diffstat (limited to 'Juick')
-rw-r--r-- | Juick/MessageCell.m | 4 | ||||
-rw-r--r-- | Juick/MessagesViewController.m | 10 | ||||
-rw-r--r-- | Juick/UIImage+Helpers.h | 15 | ||||
-rw-r--r-- | Juick/UIImage+Helpers.m | 24 |
4 files changed, 49 insertions, 4 deletions
diff --git a/Juick/MessageCell.m b/Juick/MessageCell.m index f20acbe..1dda53d 100644 --- a/Juick/MessageCell.m +++ b/Juick/MessageCell.m @@ -68,8 +68,8 @@ [self.avatar makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.contentView.top).with.offset(padding.top); make.leading.equalTo(self.contentView.leading).with.offset(padding.left); - make.width.lessThanOrEqualTo(@32); - make.height.lessThanOrEqualTo(@32); + make.width.equalTo(@32); + make.height.equalTo(@32); }]; [self.titleLabel makeConstraints:^(MASConstraintMaker *make) { diff --git a/Juick/MessagesViewController.m b/Juick/MessagesViewController.m index ac6f99d..1fb4ece 100644 --- a/Juick/MessagesViewController.m +++ b/Juick/MessagesViewController.m @@ -16,6 +16,7 @@ #import "ColorsAndButtons.h" #import "NSURL+PathParameters.h" +#import "UIImage+Helpers.h" static NSString *CellIdentifier = @"MessageCell"; @@ -153,7 +154,9 @@ static NSString *CellIdentifier = @"MessageCell"; MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; [cell updateFonts]; Message *msg = [_messages objectAtIndex:indexPath.row]; - [cell.avatar setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://i.juick.com/as/%@.png", msg.userID]]]]]; + [UIImage loadFromURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://i.juick.com/as/%@.png", msg.userID]] callback:^(UIImage *image) { + [cell.avatar setImage:image]; + }]; cell.titleLabel.text = msg.user; cell.titleLabel.textColor = [ColorsAndButtons linkColor]; cell.bodyLabel.text = msg.text; @@ -171,7 +174,10 @@ static NSString *CellIdentifier = @"MessageCell"; [cell updateFonts]; Message *msg = [_messages objectAtIndex:indexPath.row]; - [cell.avatar setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://i.juick.com/as/%@.png", msg.userID]]]]]; + [UIImage loadFromURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://i.juick.com/as/%@.png", msg.userID]] callback:^(UIImage *image) { + [cell.avatar setImage:image]; + }]; + cell.titleLabel.text = msg.user; cell.bodyLabel.text = msg.text; diff --git a/Juick/UIImage+Helpers.h b/Juick/UIImage+Helpers.h new file mode 100644 index 0000000..d30ad7b --- /dev/null +++ b/Juick/UIImage+Helpers.h @@ -0,0 +1,15 @@ +// +// UIImage+Helpers.h +// Juick +// +// Created by Vitaly Takmazov on 05.11.13. +// Copyright (c) 2013 com.juick. All rights reserved. +// + +#import <UIKit/UIKit.h> + +@interface UIImage (Helpers) + ++ (void) loadFromURL: (NSURL*) url callback:(void (^)(UIImage *image))callback; + +@end diff --git a/Juick/UIImage+Helpers.m b/Juick/UIImage+Helpers.m new file mode 100644 index 0000000..a4ae8e4 --- /dev/null +++ b/Juick/UIImage+Helpers.m @@ -0,0 +1,24 @@ +// +// UIImage+Helpers.m +// Juick +// +// Created by Vitaly Takmazov on 05.11.13. +// Copyright (c) 2013 com.juick. All rights reserved. +// + +#import "UIImage+Helpers.h" + +@implementation UIImage (Helpers) + ++ (void) loadFromURL: (NSURL*) url callback:(void (^)(UIImage *image))callback { + dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); + dispatch_async(queue, ^{ + NSData * imageData = [NSData dataWithContentsOfURL:url]; + dispatch_async(dispatch_get_main_queue(), ^{ + UIImage *image = [UIImage imageWithData:imageData]; + callback(image); + }); + }); +} + +@end |