Skip to content

Instantly share code, notes, and snippets.

@simme
Created April 20, 2011 12:21
Show Gist options
  • Save simme/931177 to your computer and use it in GitHub Desktop.
Save simme/931177 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