【问题标题】:How to change profile picture using TWRequest如何使用 TWRequest 更改头像
【发布时间】:2012-06-11 05:59:18
【问题描述】:

如何使用 TWRequest 更改头像, 我使用此代码发布到 timeLine

-(IBAction)postToTwitter { ACAccountStore *account = [[ACAccountStore alloc] init]; ACAccountType *accountType = [账户 accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

// Request access from the user to access their Twitter account
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) 
 {
     // Did user allow us access?
     if (granted == YES)
     {
         // Populate array with all available Twitter accounts
         NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];

         // Sanity check
         if ([arrayOfAccounts count] > 0) 
         {
             // Keep it simple, use the first account available
             ACAccount *acct = [arrayOfAccounts objectAtIndex:0];

             // Build a twitter request

            //http://api.twitter.com/1/account/update_profile_image.json  
             //https://upload.twitter.com/1/statuses/update_with_media.json

             TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"] parameters:nil requestMethod:TWRequestMethodPOST];

             //NSData *myData = UIImagePNGRepresentation(self.selectedImage);
            NSData *myData =  UIImageJPEGRepresentation(self.selectedImage, 0.5);

             [postRequest addMultiPartData:myData withName:@"media" type:@"image/jpg"];

             [postRequest setAccount:acct];

             // Block handler to manage the response
             /*      
              [postRequest addMultiPartData:[base64 dataUsingEncoding:NSUTF8StringEncoding] // base64 is an NSString of the encoded image

              withName:@"image"

              type:@"multipart/form-data"];

              // Post the request*/

             [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) 
              {
                  NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);
              }
              ];


     }
    }
 }];

}

【问题讨论】:

    标签: objective-c ios twitter twrequest


    【解决方案1】:

    我也为此苦苦挣扎,使用 Apple 的 WWDC11 幻灯片作为指南,但似乎 Twitter 从那时起更改了 API(或者幻灯片有错误)。

    解决方案很简单:您需要使用“图像”而不是“媒体”的多部分数据名称。而且我认为类型应该是“image/jpeg”,而不是“image/jpg”。

    所以改变那行如下:

    [postRequest addMultiPartData:myData withName:@"image" type:@"image/jpeg"];
    

    【讨论】:

      猜你喜欢
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      • 1970-01-01
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多