【问题标题】:iPhone - posting image to Tumblr using API will not actually postiPhone - 使用 API 将图像发布到 Tumblr 不会实际发布
【发布时间】:2011-04-18 02:00:15
【问题描述】:

大家好,我正在尝试使用某人在按下按钮时创建的代码向 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 在这个 modalview 被关闭后重新加载?任何建议都会很棒!

编辑: 在进行了一些 NSLog 测试后,我发现它在尝试从文件中获取图像数据后退出。我刚刚放入了我在项目中作为占位符的图像,但是我最终希望用户要么从他们的相机胶卷中选择一张照片,要么拍摄一张新照片..这可能是为什么?另外..我将如何使用 NSString 传递那种类型的图片?

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

【问题讨论】:

    标签: iphone image tumblr posting


    【解决方案1】:

    这不起作用的原因是因为我没有意识到 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];     
    

    【讨论】:

    • 感谢您发布此内容,对您很有帮助
    【解决方案2】:

    尝试回答这个问题而不是

    Sending a POST request from Cocoa to Tumblr

    【讨论】:

    • 我试图在我的项目中实现这一点,但无济于事。我已经将相机胶卷实现到 UIImageView 中。我将如何将图像更改为 NSString 以传递给函数?
    猜你喜欢
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    • 2019-12-11
    相关资源
    最近更新 更多