Developer/iOS

[IPhone] Twipic을 이용해서 이미지 업로드하기!

블로blow 2011. 1. 12. 15:27
728x90

// create the URL

NSURL *postURL = [NSURL URLWithString:@"http://twitpic.com/api/upload"];

// create the connection

NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL

// change type to POST (default is GET)

[postRequest setHTTPMethod:@"POST"];

// 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"];

// create data

NSMutableData *postBody = [NSMutableData data];

NSString *username = @"username";

NSString *password = @"password";

NSString *message = @"Testing TwitPic Upload for Blog Post";

// 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]];

// 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=\"dummy.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]];

// get the image data from main bundle directly into NSData object

NSString *imagePath = [[NSBundle mainBundlepathForResource:@"icon" ofType:@"png"];

NSData *imageData = [NSData dataWithContentsOfFile:imagePath];

// 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];

// Request 사용하여 실제 연결을 시도하는 NSURLConnection 인스턴스 생성

NSURLConnection *connection = [[NSURLConnection allocinitWithRequest:postRequest delegate:self];


// 정상적으로 연결이 되었다면

if(connection)

{

NSLog(@"<< 요청 성공(POST) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"nil);

// 데이터를 전송받을 멤버 변수 초기화

receivedData = [[NSMutableData allocinit];

return YES;

}

NSLog(@"<< 요청 실패(POST) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"nil);


return NO;

 

728x90