2011-04-18 47 views
0

嗨,大家好,我正尝试使用某人在按钮按下时创建的代码发布图像类型的帖子到Tumblr,但是当按下按钮时,NSLogs不显示。如果情况有所不同,那么接收信息的观点就是一种模态观点。iPhone - 使用API​​发布图像到Tumblr实际上不会发布

- (IBAction)createPost { 
    NSString *caption = @"This is a test"; 
    [self sendPhotoToTumblr:UIImagePNGRepresentation(foodImage.image) usingEmail:email andPassword:password withCaption:caption]; 
    [self dismissModalViewControllerAnimated:YES]; 
} 

- (BOOL)sendPhotoToTumblr:(NSData *)photo usingEmail:(NSString *)tumblrEmail andPassword:(NSString *)tumblrPassword withCaption:(NSString *)caption; 
{ 
    //get image data from file 
    NSData *imageData = 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; 
} 

我感觉好像我的问题是从我不能够理解的API加的事实,我真的不明白NSURLRequests像我已经阅读了进去。此外,有没有办法让这个modalview被解雇后的UIWebView重新加载?任何建议都会很棒!

编辑:在做了一些NSLog测试后,我发现在它试图从文件中获取图像数据后,它会保留。我刚刚在项目中放置了一个作为占位符的图像,但是我最终希望让用户从他们的相机胶卷中挑选一张照片或者拍摄一张新照片。这是为什么?另外..如何使用NSString传递该类型的图片?

编辑(再):我改变了sendPhotoToTumblr接受NSData的照片,而不是一个字符串,因为我不知道如何传递图像作为一个字符串。我已经通过了整个代码,它说“请求已提交”,但没有发布任何内容。我知道receivedData包含Tumblr响应的HTTP错误代码,但是我不知道如何打印它。思考?在这个问题上

回答

2

这是不工作的原因是因为我没有意识到的是,iPhone存储的图像作为.jpgs而不是巴纽。

[self sendPhotoToTumblr:UIImageJPEGRepresentation(foodImage.image, 0.5) usingEmail:email andPassword:password withCaption:caption]; 

,并更改sendPhotoToTumblr来

- (BOOL)sendPhotoToTumblr:(NSData *)photo usingEmail:(NSString *)tumblrEmail andPassword:(NSString *)tumblrPassword withCaption:(NSString *)caption; 
{ 
    //get image data from file 
    //NSData *imageData = photo; 
    //stop on error 
    if (!photo) 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:photo]; 
    [keysDict release];  
+0

感谢您发布此信息,这非常有帮助 – jjxtra 2013-01-01 18:12:11

0
+0

我试图落实到我的项目,今年年初,但无济于事。我已经将相机卷装入UIImageView中。我将如何将图像转换为NSString传递给函数? – 2011-04-18 07:55:16