嘿伙计们,我正在尝试使用某人在按钮按下时创建的代码向 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 错误代码,但我不知道如何打印它。想法?