首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用TWRequest更改头像

如何使用TWRequest更改头像
EN

Stack Overflow用户
提问于 2012-06-06 01:37:06
回答 1查看 651关注 0票数 1

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

-(IBAction)postToTwitter { ACAccountStore *account = [ACAccountStore分配初始化];ACAccountType * account init=account account

代码语言:javascript
复制
// 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]);
              }
              ];


     }
    }
 }];

}

EN

回答 1

Stack Overflow用户

发布于 2012-06-11 05:59:19

我也为此而苦苦挣扎,使用苹果的WWDC11幻灯片作为指南,但似乎推特从那时起就改变了API (或者幻灯片有一个bug)。

解决方案很简单:您需要使用由多个部分组成的数据名称"image“而不是"media”。我认为类型应该是"image/jpeg",而不是"image/jpg“。

因此,请按如下方式更改该行:

代码语言:javascript
复制
[postRequest addMultiPartData:myData withName:@"image" type:@"image/jpeg"];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10902120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档