iPhone - 使用API​​向Tumblr发布图片实际上不会发布

时间:2011-04-18 02:00:15

标签: iphone image tumblr posting

嘿伙计们,我试图在按钮按下时使用某人创建的代码向Tumblr发布和图像类型发布,但是当按下按钮时,NSLogs不会显示。接收信息的视图是模态视图,如果有任何不同的视图。

- (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的事实,就像我已经读过它一样。此外,还有一种方法可以在解除此模态视图后重新加载UIWebView吗?任何建议都会很棒!

编辑:在做了一些NSLog测试之后,我发现在尝试从文件中获取图像数据后它会失效。我刚刚在项目中添加了一个作为占位符的图像,但我最终希望让用户从相机胶卷中选择一张照片或者换一张新照片。这可能就是为什么?另外..我会用NSString传递那种类型的图片吗?

编辑(再次):我将sendPhotoToTumblr更改为接受照片的NSData而不是字符串,因为我无法弄清楚如何将图像作为字符串传递。我已经完成了整个代码并且它说“请求提交”但是没有发布任何内容。我知道receivedData包含Tumblr响应的HTTP错误代码,但是我不知道如何打印它。想法?

2 个答案:

答案 0 :(得分:2)

这不起作用的原因是因为我没有意识到iPhone将图像存储为.jpgs而不是.png。

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

答案 1 :(得分:0)

尝试在这个问题中回答问题

Sending a POST request from Cocoa to Tumblr