From c3750f7891ab73ed4c4bfa8ddc59969fe77b0d9e Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 5 Nov 2013 22:25:33 +0400 Subject: load avatars async --- Juick.xcodeproj/project.pbxproj | 6 ++++++ Juick/MessageCell.m | 4 ++-- Juick/MessagesViewController.m | 10 ++++++++-- Juick/UIImage+Helpers.h | 15 +++++++++++++++ Juick/UIImage+Helpers.m | 24 ++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 Juick/UIImage+Helpers.h create mode 100644 Juick/UIImage+Helpers.m diff --git a/Juick.xcodeproj/project.pbxproj b/Juick.xcodeproj/project.pbxproj index 7add9f1..21f3035 100644 --- a/Juick.xcodeproj/project.pbxproj +++ b/Juick.xcodeproj/project.pbxproj @@ -31,6 +31,7 @@ 77C67EEC18283F2D00427098 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 77C67EEB18283F2D00427098 /* QuartzCore.framework */; }; 77C67EF21828442C00427098 /* UIImage+Crop.m in Sources */ = {isa = PBXBuildFile; fileRef = 77C67EF11828442C00427098 /* UIImage+Crop.m */; }; 77D163C2181F048F00401EF0 /* MessageCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 77D163C1181F048F00401EF0 /* MessageCell.m */; }; + 77DC49E818296EA300A1136A /* UIImage+Helpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 77DC49E718296EA300A1136A /* UIImage+Helpers.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -78,6 +79,8 @@ 77C67EF11828442C00427098 /* UIImage+Crop.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+Crop.m"; sourceTree = ""; }; 77D163C0181F048F00401EF0 /* MessageCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MessageCell.h; sourceTree = ""; }; 77D163C1181F048F00401EF0 /* MessageCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MessageCell.m; sourceTree = ""; }; + 77DC49E618296EA300A1136A /* UIImage+Helpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+Helpers.h"; sourceTree = ""; }; + 77DC49E718296EA300A1136A /* UIImage+Helpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+Helpers.m"; sourceTree = ""; }; AD52EBAFFD6B4395B342F246 /* Pods-Juick.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Juick.xcconfig"; path = "Pods/Pods-Juick.xcconfig"; sourceTree = ""; }; E1A04338EB0146778B4503DC /* libPods-Juick.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-Juick.a"; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ @@ -165,6 +168,8 @@ 77C67EE71828379700427098 /* ColorsAndButtons.m */, 77C67EF01828442C00427098 /* UIImage+Crop.h */, 77C67EF11828442C00427098 /* UIImage+Crop.m */, + 77DC49E618296EA300A1136A /* UIImage+Helpers.h */, + 77DC49E718296EA300A1136A /* UIImage+Helpers.m */, ); path = Juick; sourceTree = ""; @@ -341,6 +346,7 @@ 77C67EE81828379700427098 /* ColorsAndButtons.m in Sources */, 77C67EE51828342000427098 /* NSURL+PathParameters.m in Sources */, 77317BB8181BBE8500D60005 /* main.m in Sources */, + 77DC49E818296EA300A1136A /* UIImage+Helpers.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; 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 + +@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 -- cgit v1.2.3