博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Posting images using TWRequest《…
阅读量:4072 次
发布时间:2019-05-25

本文共 2685 字,大约阅读时间需要 8 分钟。

Posting images using TWRequest

(https://dev.twitter.com/docs/ios/posting-images-using-twrequest)
Updated on Wed, 2012-02-01 17:53

Overview

The Twitter framework's TWRequest encapsulates calls to the Twitter API, including signing the request on behalf of the user. With Twitter's photo hosting service, and its API endpoint , it is possible to post a Tweet and a status simultaneously via a TWRequest.

Code Example

Before posting a Tweet with an attached image, you should check  to check thecharacters_reserved_per_media and max_media_per_upload values. The former tells you how many characters out of 140 you should reserve for the URL of the posted image, while the latter tells you the number of images that you can attach to the Tweet.

Once you know the current Twitter configuration, you can post the Tweet. You use the TWRequest just as you would for any Twitter API call, but make sure that both the data and the status' text as "multipart/form-data."

Notes:

  • This example utilizes .
  • Extraneous line breaks are added for clarity
  •  

  1. - (void)performTWRequestUpload
  2. {
  3. NSURL *url =
  4. [NSURL URLWithString:
  5. @"https://upload.twitter.com/1/statuses/update_with_media.json"];
  6.  
  7. //  Create a POST request for the target endpoint
  8. TWRequest *request =
  9. [[TWRequest alloc] initWithURL:url
  10. parameters:nil
  11. requestMethod:TWRequestMethodPOST];
  12.  
  13. //  self.accounts is an array of all available accounts;
  14. //  we use the first one for simplicity
  15. [request setAccount:[self.accounts objectAtIndex:0]];
  16.  
  17. //  The "larry.png" is an image that we have locally
  18. UIImage *image = [UIImage imageNamed:@"larry.png"];
  19.  
  20. //  Obtain NSData from the UIImage
  21. NSData *imageData = UIImagePNGRepresentation(image);
  22.  
  23. //  Add the data of the image with the
  24. //  correct parameter name, "media[]"
  25. [request addMultiPartData:imageData
  26. withName:@"media[]"
  27. type:@"multipart/form-data"];
  28.  
  29. // NB: Our status must be passed as part of the multipart form data
  30. NSString *status = @"just setting up my twttr #iOS5";
  31.  
  32. //  Add the data of the status as parameter "status"
  33. [request addMultiPartData:[status dataUsingEncoding:NSUTF8StringEncoding]
  34. withName:@"status"
  35. type:@"multipart/form-data"];
  36.  
  37. //  Perform the request.
  38. //    Note that -[performRequestWithHandler] may be called on any thread,
  39. //    so you should explicitly dispatch any UI operations to the main thread
  40. [request performRequestWithHandler:
  41. ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
  42. NSDictionary *dict =
  43. (NSDictionary *)[NSJSONSerialization
  44. JSONObjectWithData:responseData options:0 error:nil];
  45.  
  46. // Log the result
  47. NSLog(@"%@", dict);
  48.  
  49. dispatch_async(dispatch_get_main_queue(), ^{
  50. // perform an action that updates the UI...
  51. });
  52. }];
  53. }

转载地址:http://xjeji.baihongyu.com/

你可能感兴趣的文章
Unix + OS IBM Aix System Director
查看>>
Unix + OS IBM Aix FTP / wu-ftp / proftp
查看>>
my read work
查看>>
db db2 base / instance database tablespace container
查看>>
hd disk / disk raid / disk io / iops / iostat / iowait / iotop / iometer
查看>>
project ASP.NET
查看>>
db db2_monitorTool IBM Rational Performace Tester
查看>>
OS + Unix Aix telnet
查看>>
IBM Lotus
查看>>
Linux +Win LAMPP Tools XAMPP 1.7.3 / 5.6.3
查看>>
my read_university
查看>>
network manager
查看>>
OS + Linux Disk disk lvm / disk partition / disk mount / disk io
查看>>
RedHat + OS CPU、MEM、DISK
查看>>
net TCP/IP / TIME_WAIT / tcpip / iperf / cain
查看>>
webServer kzserver/1.0.0
查看>>
hd printer lexmark / dazifuyin / dayin / fuyin
查看>>
OS + Unix IBM Aix basic / topas / nmon / filemon / vmstat / iostat / sysstat/sar
查看>>
my ReadMap subway / metro / map / ditie / gaotie / traffic / jiaotong
查看>>
OS + Linux DNS Server Bind
查看>>