Как изменить изображение профиля с помощью TWRequest

Как изменить изображение профиля с помощью TWRequest, я отправляю на timeLine, используя этот код

- (IBAction) postToTwitter {ACAccountStore * account = [[ACAccountStore alloc] init]; ACAccountType * accountType = [account 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]);
              }
              ];


     }
    }
 }];

}

1 ответ

Я тоже боролся с этим, используя слайды Apple WWDC11 в качестве руководства, но кажется, что Twitter изменил API с тех пор (или на слайде была ошибка).

Решение простое: вам нужно использовать многоэлементное имя данных "изображение" вместо "медиа". И я думаю, что тип должен быть "image/jpeg", а не "image/jpg".

Поэтому измените эту строку следующим образом:

[postRequest addMultiPartData:myData withName:@"image" type:@"image/jpeg"];
Другие вопросы по тегам