iPhone - 使用 API 将图像发布到 Tumblr 实际上不会发布
大家好,我正在尝试使用某人在按下按钮时创建的代码向 Tumblr 发布图像类型的帖子,但是按下按钮时 NSLog 不会显示。如果有什么不同的话,接受信息的视图是模态视图。
- (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 而不是字符串,因为我不知道如何将图像作为字符串传递。我已经让它检查了整个代码,它显示“请求已提交”,但没有发布任何内容。我知道 receiveData 包含 Tumblr 响应的 HTTP 错误代码,但我不知道如何打印它。想法?
Hey guys, I am attempting to post and image-type post to Tumblr using someone's created code in a button press however when the button is pressed the NSLogs do not show up. The view that takes in the information is a modal view, if that is any different.
- (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;
}
I feel as though my problems are from me not being able to understand the API plus the fact that I don't really understand NSURLRequests as much as I have read into it. Also, is there a way to have the UIWebView reload after this modalview is dismissed? Any advice would be great!
EDIT: After doing some NSLog testing, I find out that it bails out after it attempts to get the image data from file. I had just put in an image that I had in the project as a placeholder, however I eventually wish to have the user either pick a photo from their camera roll or take a new one..could this be why? Also..how would I pass in that type of picture using an NSString?
EDIT (AGAIN): I changed the sendPhotoToTumblr to accept NSData of a photo instead of a string because I could not figure out how to pass in the image as a string. I have had it go through the whole code and it says "Request Submitted" however nothing is posted. I know receivedData contains the HTTP error code that Tumblr responds with, however I do not know how to print it. Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不起作用的原因是我没有意识到 iPhone 将图像存储为 .jpg 而不是 .png。
并将 sendPhotoToTumblr 的开头更改为
The reason this was not working is because I did not realize that the iPhone stored the images as .jpgs instead of .png.
And change the beginning of sendPhotoToTumblr to
尝试回答这个问题,而不是
从 Cocoa 向 Tumblr 发送 POST 请求
try answer in this question istead
Sending a POST request from Cocoa to Tumblr