Загрузить / отправить изображения в Twitter / MySpace с iphone - PullRequest
1 голос
/ 07 апреля 2011

Мне нужно загрузить изображение из приложения iphone в твиттер и на MySpace.Я пытался, но не нашел никакого решения.Любой может помочь.Особенно, если кто-то может указать образец кода.

Заранее спасибо

Ответы [ 2 ]

5 голосов
/ 07 апреля 2011

Вы можете использовать http://dev.twitpic.com/

Разместите ваше изображение в http запросе здесь.

Вот код -

-(void)postToTwitter
{

// create the URL
NSURL *postURL = [NSURL URLWithString:@"http://api.twitpic.com/1/uploadAndPost.xml"];

// create the connection
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:30.0];

// change type to POST (default is GET)
[postRequest setHTTPMethod:@"POST"];



// create data
NSMutableData *postBody = [NSMutableData data];

NSString *username = emailTextField.text;
NSString *password = passTextField.text;

NSString *consumer_token=@"consumer token";
NSString *consumer_secret=@"consumer secret ";
NSString *oauth_token=@"oauth token";
NSString *oauth_secret=@"oauth secret";
NSString *api_key=@"api key";

NSString *message = commentTxt.text;


// just some random text that will never occur in the body
NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
// header value
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
// set header
[postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];

// username part
 [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[username dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

 // password part
 [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"password\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[password dataUsingEncoding:NSUTF8StringEncoding]];
 [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


// api_key
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[api_key dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// consumer_token
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"consumer_token\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[consumer_token dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// consumer_secret
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"consumer_secret\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[consumer_secret dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

//oauth_token
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"oauth_token\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[oauth_token dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

//oauth_secret
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"oauth_secret\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[oauth_secret dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// message part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"message\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[message dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


// media part
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name=\"media\"; filename=\"fish.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

img=[cFun correctImageOrientation:img];
NSData *imageData = UIImageJPEGRepresentation(img, 90);

// add it to body
[postBody appendData:imageData];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// final boundary
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];


// add body to post
[postRequest setHTTPBody:postBody];

// pointers to some necessary objects
//NSURLResponse* response;
//NSError* error;
[activity_bg setHidden:NO];
[activityIndicator startAnimating];

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:postRequest delegate:self];

if( theConnection )
{
    webData = [[NSMutableData data] retain];
}
else
{
    NSLog(@"theConnection is NULL");
}

}

Надеюсь, это поможет вам:)

2 голосов
/ 07 апреля 2011

Вы не можете загружать изображения прямо в твиттер. Вам нужно использовать сервис изображений, такой как twitpic, который будет иметь свой собственный API - http://twitpic.com/api.do (есть много альтернатив, например, yfrog).

Вы можете UIImagePickerController получить изображение, а затем просто написать код для публикации в выбранном вами сервисе изображений.

...