Well, lets say that you want to display the Twitter accounts that the user has on his device in the table. You probably want to display the avatar in a table cell, in which case you will need to request the Twitter API.
Assuming you have NSArrayobjects ACAccount, you can create a dictionary to store additional profile information for each account. The tableView:cellForRowAtIndexPath:following code will be required for your table view controller :
UIImage *profileImage = nil;
NSDictionary *info = [self.twitterProfileInfos objectForKey:account.identifier];
if (info) profileImage = [info objectForKey:kTwitterProfileImageKey];
if (profileImage) {
cell.imageView.image = profileImage;
} else {
[self getTwitterProfileImageForAccount:account completion:^ {
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}];
}
, , UIImage , , NSString. , , , . :
#pragma mark - Twitter
- (void)getTwitterProfileImageForAccount:(ACAccount *)account completion:(void(^)(void))completion {
NSURL *url = [NSURL URLWithString:@"users/profile_image" relativeToURL:kTwitterApiRootURL];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
account.username, @"screen_name",
@"bigger", @"size",
nil];
TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!responseData && error) {
abort();
}
UIImage *profileImg = [UIImage imageWithData:responseData];
NSMutableDictionary *info = [self.twitterProfileInfos objectForKey:account.identifier];
if (!info) {
info = [NSMutableDictionary dictionary];
[self.twitterProfileInfos setObject:info forKey:account.identifier];
}
[info setObject:profileImg forKey:kTwitterProfileImageKey];
if (completion) dispatch_async(dispatch_get_main_queue(), completion);
}];
}
, , , , . .
Twitter, .