Nun, lässt sagt du uns angezeigt werden soll Konten der Benutzer auf dem Gerät in einer Tabelle hat. Wahrscheinlich möchten Sie den Avatar in der Tabellenzelle anzeigen. In diesem Fall müssen Sie die API von Twitter abfragen.
Angenommen, Sie haben einen NSArray
von ACAccount
Objekte, können Sie ein Wörterbuch erstellen, um zusätzliche Profilinformationen für jedes Konto zu speichern. Ihre Tabellenansicht Controller tableView:cellForRowAtIndexPath:
würde einige Codes wie dieser braucht:
// Assuming that you've dequeued/created a UITableViewCell...
// Check to see if we have the profile image of this account
UIImage *profileImage = nil;
NSDictionary *info = [self.twitterProfileInfos objectForKey:account.identifier];
if (info) profileImage = [info objectForKey:kTwitterProfileImageKey];
if (profileImage) {
// You'll probably want some neat code to round the corners of the UIImageView
// for the top/bottom cells of a grouped style `UITableView`.
cell.imageView.image = profileImage;
} else {
[self getTwitterProfileImageForAccount:account completion:^ {
// Reload this row
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}];
}
All dies tut, ist Zugriff auf ein UIImage
Objekt aus einem Wörterbuch des Wörterbücher, verkeilte durch die Kontokennung und dann ein statischen NSString
Schlüssel. Wenn es kein Bildobjekt erhält, ruft es eine Instanzmethode auf und übergibt einen Completion-Handler-Block, der die Tabellenzeile erneut lädt. Die Instanzmethoden sieht ein bisschen wie folgt aus:
#pragma mark - Twitter
- (void)getTwitterProfileImageForAccount:(ACAccount *)account completion:(void(^)(void))completion {
// Create the URL
NSURL *url = [NSURL URLWithString:@"users/profile_image" relativeToURL:kTwitterApiRootURL];
// Create the parameters
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
account.username, @"screen_name",
@"bigger", @"size",
nil];
// Create a TWRequest to get the the user's profile image
TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET];
// Execute the request
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
// Handle any errors properly, not like this!
if (!responseData && error) {
abort();
}
// We should now have some image data
UIImage *profileImg = [UIImage imageWithData:responseData];
// Get or create an info dictionary for this account if one doesn't already exist
NSMutableDictionary *info = [self.twitterProfileInfos objectForKey:account.identifier];
if (!info) {
info = [NSMutableDictionary dictionary];
[self.twitterProfileInfos setObject:info forKey:account.identifier];
}
// Set the image in the profile
[info setObject:profileImg forKey:kTwitterProfileImageKey];
// Execute our own completion handler
if (completion) dispatch_async(dispatch_get_main_queue(), completion);
}];
}
Also, stellen Sie sicher, dass Sie anmutig scheitern, aber, dass dann die Tabelle aktualisieren, da sie die Profilbilder herunterlädt. In Ihrem Completion-Handler können Sie diese in einen Image-Cache stellen oder sie auf andere Weise über die Lebensdauer der Klasse hinaus beibehalten.
Das gleiche Verfahren könnte verwendet werden, um auf andere Twitter-Benutzerinformationen zuzugreifen, see their docs.
Vielen Dank für ur Antwort ... aber ich brauche die Antwort als json von Profilinformationen des Benutzers ... –
@RahulNair leider meine Kristallkugel mir nicht erzählen, dass – AnthonyBlake