Skip to content

Instantly share code, notes, and snippets.

@siqin
Forked from simme/gcd.m
Created March 9, 2012 03:02
Show Gist options
  • Save siqin/2004799 to your computer and use it in GitHub Desktop.
Save siqin/2004799 to your computer and use it in GitHub Desktop.
Loading images async with GCD
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSString *tweetText = [_tweet valueForKey:@"text"];
NSString *userName = [_tweet valueForKeyPath:@"user.name"];
NSString *userDescription = [_tweet valueForKeyPath:@"user.description"];
NSNumber *friends = [_tweet valueForKeyPath:@"user.friends_count"];
NSNumber *followers = [_tweet valueForKeyPath:@"user.followers_count"];
// Checkin'
if ( (NSNull *)userDescription == [NSNull null] ) {
userDescription = @"";
}
self.userNameTextLabel.text = userName;
self.userDescriptionTextView.text = userDescription;
self.friendsCountLabel.text = [friends stringValue];
self.followersCountLabel.text = [followers stringValue];
self.tweetTextView.text = tweetText;
// Load images async
dispatch_queue_t queue = dispatch_queue_create("com.app.task",NULL);
dispatch_queue_t main = dispatch_get_main_queue();
dispatch_async(queue, ^{
NSString *urlString = [[_tweet valueForKeyPath:@"user.profile_image_url"] stringByReplacingOccurrencesOfString:@"_normal" withString:@"_bigger"];
NSURL *url = [NSURL URLWithString:urlString];
UIImage *profile = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
dispatch_async(main, ^{
self.profileImageView.image = profile;
[self.profileImageLoading stopAnimating];
});
});
dispatch_async(queue, ^{
NSString *urlString = [_tweet valueForKeyPath:@"user.profile_background_image_url"];
NSURL *url = [NSURL URLWithString:urlString];
UIImage *backgroundImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
dispatch_async(main, ^{
self.background.image = backgroundImage;
});
});
dispatch_release(queue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment