iPhone-使用API向Tumblr发布图像实际上不会发布
||
大家好,我正在尝试使用某人在按钮中按下创建的代码将图像和图像类型的文章发布到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的了解不如我所读到的那么多。另外,有没有办法在取消此modalview后重新加载UIWebView?任何建议都很好!
编辑:做过一些NSLog测试后,我发现它尝试从文件中获取图像数据后无法使用。我刚刚放入了一个在项目中作为占位符使用的图像,但是我最终希望让用户从他们的相机胶卷中选择一张照片,或者换一张新的。另外..我将如何使用NSString传递此类图片?
编辑(再次):我将sendPhotoToTumblr更改为接受照片的NSData而不是字符串,因为我不知道如何将图像作为字符串传递。我已经遍历了整个代码,并显示“请求提交”,但是没有任何内容。我知道ReceivedData包含Tumblr响应的HTTP错误代码,但是我不知道如何打印它。有什么想法吗?
没有找到相关结果
已邀请:
2 个回复
臀博
并将sendPhotoToTumblr的开头更改为
抵舵