2011-03-15 5 views
0

안녕하세요. 나는 텀블러에서 상태 업데이트를했는데 아래 그림과 같이 사진을 데이터 형태로 보낼 때 문제가 있습니다.tumblr iphone integration

-(IBAction)sendPhoto 
{ 
    NSString *email   = @"[email protected]"; 
    NSString *password  = @"password"; 
    NSString *sendType = @"photo"; 

    UIImage *imageMS = [UIImage imageNamed:@"Submit.png"]; 
    NSData *photoData = [[NSData alloc] initWithData:UIImagePNGRepresentation(imageMS)]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 
           initWithURL:[NSURL URLWithString:@"http://www.tumblr.com/api/write"]]; 
    [request setHTTPMethod:@"POST"]; 
    NSString *request_body = [NSString 
      stringWithFormat:@"email=%@&password=%@&type=%@&data=%@", 
      [email   stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], 
      [password  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], 
      [sendType  stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], 
      photoData]; 
    [request setHTTPBody:[request_body dataUsingEncoding:NSUTF8StringEncoding]]; 
    [web loadRequest:request]; 
} 

그러나 업데이트되지 않습니다. 왜?

+0

또한 실제 사용자 이름과 암호를 게시해서는 안되며, 즉시 변경하는 것이 좋습니다. – theChrisKent

+0

괜찮 았으면 변경 될 것입니다. 알고 계시다면 사진을 tumblr에 업로드하는 것과 관련하여 도움을 주시기 바랍니다. –

답변

2

내 원래의 대답이 틀린 것으로 판명되었지만 OP가 자신의 문제를 해결했다고 말한 코드에 대한 링크를 게시했습니다. 나는 그 코드를 아래의 코드를 복사하여 장래의 검색자가 쉽게 찾을 수 있도록했습니다. 나는 당신의 문제를 설명하는 것이 얼마나 실망 스러운지 알기 때문에 해결책에 대한 링크 만 발견하면 그 링크는 죽을 수 있습니다. http://forums.macrumors.com/showthread.php?t=427513에 따라

코드 : 나는 몇 가지 메모리 문제를 제거하고이 유연한/더 일반적인 솔루션입니다 몇 가지 매개 변수를 추가하려면 위의 코드를 수정 한

- (BOOL)sendPhotoToTumblr:(NSString *)photo usingEmail:(NSString *)tumblrEmail andPassword:(NSString *)tumblrPassword withCaption:(NSString *)caption; 
{ 
    //get image data from file 
    NSData *imageData = [NSData dataWithContentsOfFile:photo]; 
    //stop on error 
    if (!imageData) return NO; 

    //Create dictionary of post arguments 
    NSArray *keys = [NSArray arrayWithObjects:@"email",@"password",@"type",@"caption",nil]; 
    NSArray *objects = [NSArray arrayWithObjects: 
      tumblrEmail, 
      tumblrPassword, 
      @"photo", caption, nil]; 
    NSDictionary *keysDict = [[NSDictionary alloc] initWithObjects:objects forKeys:keys]; 

    //create tumblr photo post 
    NSURLRequest *tumblrPost = [self createTumblrRequest:keysDict withData:imageData]; 
    [keysDict release];  

    //send request, return YES if successful 
    tumblrConnection = [[NSURLConnection alloc] initWithRequest:tumblrPost delegate:self]; 
    if (!tumblrConnection) { 
     NSLog(@"Failed to submit request"); 
     return NO; 
    } else { 
     NSLog(@"Request submitted"); 
     receivedData = [[NSMutableData data] retain]; 
      [tumblrConnection release]; 
     return YES; 
    } 
} 


-(NSURLRequest *)createTumblrRequest:(NSDictionary *)postKeys withData:(NSData *)data 
{ 
    //create the URL POST Request to tumblr 
    NSURL *tumblrURL = [NSURL URLWithString:@"http://www.tumblr.com/api/write"]; 
    NSMutableURLRequest *tumblrPost = [NSMutableURLRequest requestWithURL:tumblrURL]; 
    [tumblrPost setHTTPMethod:@"POST"]; 

    //Add the header info 
    NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"]; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary]; 
    [tumblrPost addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

    //create the body 
    NSMutableData *postBody = [NSMutableData data]; 
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    //add key values from the NSDictionary object 
    NSEnumerator *keys = [postKeys keyEnumerator]; 
    int i; 
    for (i = 0; i < [postKeys count]; i++) { 
     NSString *tempKey = [keys nextObject]; 
     [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",tempKey] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postBody appendData:[[NSString stringWithFormat:@"%@",[postKeys objectForKey:tempKey]] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    } 

    //add data field and file data 
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"data\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postBody appendData:[NSData dataWithData:data]]; 
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    //add the body to the post 
    [tumblrPost setHTTPBody:postBody]; 

    return tumblrPost; 
} 

. 그러나 누군가가 원본 코드를 해당 사이트에 게시하고 싶다면이 답변의 수정 버전을 살펴보십시오.

+0

이 코드를 추가했지만 photoString의 내용이 null이 됨 –

+0

이제 사진을 성공적으로 보낼 수 있습니다.이 링크의 코드를 사용하고 있습니다. http://forums.macrumors.com/showthread.php?t=427513. –

+0

텀블러를위한 다운로드 ios SDK에 대한 링크를 제공 할 수 있습니까? – kb920

0

나중에 참조 할 수 있도록 ASIHTTPRequest의 ASIFormDatRequest는 이러한 일을 훨씬 쉽게 만듭니다.

관련 문제