我有一個訪問Twitter供稿並將文本放入表格的代碼。然後我編輯了代碼,這樣我就可以在不同的視圖中以自定義的方式顯示文本,但我也想從tweets中獲取圖像,儘管一個多小時搜索無法找到單個引用。我已經看過如何「發佈」圖片,但要清楚,我需要從相關推文中獲取並「顯示」圖片。你如何從Twitter供稿中獲取「圖片」以顯示在iOS應用中
下面是我的代碼的亮點,處理Twitter的訪問:
-(void)twitterTimeLine
{
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted == YES)
{
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0)
{
ACAccount *twitterAccount = [arrayOfAccounts lastObject]; // last account on list of accounts
NSURL *requestAPI = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json"];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setObject:@"30" forKey:@"count"];
[parameters setObject:@"1" forKey:@"incude_entities"];
SLRequest *posts = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:requestAPI parameters:parameters];
posts.account = twitterAccount;
[posts performRequestWithHandler:^(NSData *response, NSHTTPURLResponse *urlResponse, NSError *error) {
if (response)
{
// TODO: might want to check urlResponse.statusCode to stop early
NSError *jsonError; // use new instance here, you don't want to overwrite the error you got from the SLRequest
NSArray *array =[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&jsonError];
if (array) {
if ([array isKindOfClass:[NSArray class]]) {
self.array = array;
NSLog(@"resulted array: %@",self.array);
}
else {
// This should never happen
NSLog(@"Not an array! %@ - %@", NSStringFromClass([array class]), array);
}
}
else {
// TODO: Handle error in release version, don't just dump out this information
NSLog(@"JSON Error %@", jsonError);
NSString *dataString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(@"Received data: %@", dataString ? dataString : response); // print string representation if response is a string, or print the raw data object
}
}
else {
// TODO: show error information to user if request failed
NSLog(@"request failed %@", error);
}
self.array = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
if (self.array.count != 0)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData]; // this part loads into table - important!
});
}
}];
}
}
else
{
NSLog(@"%@", [error localizedDescription]);
}
}];
}
,這裏是我怎麼顯示鳴叫
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
NSDictionary *tweet = _array[indexPath.row];
cell.textLabel.text = tweet[@"text"];
//NSString *element = [myArray objectAtIndex:2];
//NSString *element = myArray[2];
// I created some custom views to show the text, but kept the table for testing purposes
TemplateView *tempView = [viewArray objectAtIndex:testCounter];
tempView.TweetView.text = tweet[@"text"];
// -> this was what I was hoping for // tempView.ContentView.image = tweet[@"image"];
testCounter++;
if (testCounter >= 30)
{
testCounter = 0;
}
return cell;
}
我拿出我認爲是其中的關鍵線我需要看看:
tempView.TweetView.text = tweet[@"text"];
tempView.ContentView.image = tweet[@"image"];
//希望後者將作爲第一個工作,但c學習它並不是那麼簡單
這可能是不可能的,如果是這樣,我將如何從「鏈接」(url)獲取圖像並確保它是圖像而不是視頻或其他網站?
我就可以建立一個「詞搜索」搶文本從鳴叫以http並希望從字符串生成一個URL
您的示例鏈接不起作用?但我正在檢查開發鏈接。我幾乎已經辭職了,只是讓它離開它,謝謝,我會研究這個,看看如何實現它 –
@DanielMorrison:示例鏈接只有在您正確驗證後才能使用。這個例子只從開發鏈接中給出。您可以按照以下方式嘗試開發鏈接:首先,您通過「OAuth 1」進行授權,然後選擇搜索API方法(/search/tweets.json)。在「查詢標籤」之後,在「q」字段中輸入「nature」或任何關鍵字,然後在「include_entities」字段下輸入「true」值。在「自定義/名稱」下,將名稱設置爲「過濾器」並將其值設爲「圖像」。現在點擊「發送」按鈕。你會得到上面提到的示例URL,也需要結果。希望它會給你更多的想法.. :) – Sandeep