2015-05-14 26 views
0

我有一個訪問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

回答

0

我們可以通過應用「過濾器=圖片」查詢與獲取與圖像鳴叫「 include_entities =真」。它會向媒體實體發送推文,我們可以在其中看到type =「photo」和其他相關數據。

對於前: https://api.twitter.com/1.1/search/tweets.json?q=nature&include_entities=true&filter=images

嘗試在Twitter的開發者控制檯此查詢並查看響應格式:https://dev.twitter.com/rest/tools/console

希望這會有所幫助。

+0

您的示例鏈接不起作用?但我正在檢查開發鏈接。我幾乎已經辭職了,只是讓它離開它,謝謝,我會研究這個,看看如何實現它 –

+0

@DanielMorrison:示例鏈接只有在您正確驗證後才能使用。這個例子只從開發鏈接中給出。您可以按照以下方式嘗試開發鏈接:首先,您通過「OAuth 1」進行授權,然後選擇搜索API方法(/search/tweets.json)。在「查詢標籤」之後,在「q」字段中輸入「nature」或任何關鍵字,然後在「include_entities」字段下輸入「true」值。在「自定義/名稱」下,將名稱設置爲「過濾器」並將其值設爲「圖像」。現在點擊「發送」按鈕。你會得到上面提到的示例URL,也需要結果。希望它會給你更多的想法.. :) – Sandeep

1

TwitterKit似乎不公開支持圖像..我有同樣的愚蠢問題。當使用內置tableview和數據源時,API內部保存圖像。它需要一個listID和一個Slug ..但是,當你想要通過JSON的圖像,你是不走運的!即使TWTRTweet對象也沒有實體或媒體屬性!

不知道怎麼會有人開發這樣一個可怕的API。在任何情況下,我做顛倒了服務器內部調用,並發現其發送等「無證」參數..

例子:

TWTRAPIClient *client = [[TWTRAPIClient alloc] init]; 

NSString *endpoint = @"https://api.twitter.com/1.1/statuses/user_timeline.json"; 

NSDictionary *params = @{@"screen_name":@"SCREEN_NAME_HERE", 
          @"count": @"30"}; 

將返回NO MEDIA ..即使您有@"include_entities" : @"true"

解決方案:

TWTRAPIClient *client = [[TWTRAPIClient alloc] init]; 

NSString *endpoint = @"https://api.twitter.com/1.1/statuses/user_timeline.json"; 

NSDictionary *params = @{@"screen_name":@"SCREEN_NAME_HERE", 
         @"count": @"30", 
         @"tweet_mode": @"extended"}; 

隨着tweet_mode集到extendedtweet_mode是一個未記錄參數),它會現在回到媒體作爲響應。這包括「類型」,它是「照片」的一部分圖像。

相關問題