Created
April 20, 2011 12:21
-
-
Save simme/931177 to your computer and use it in GitHub Desktop.
Loading images async with GCD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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