在iPhone的tumblr上上传图像
我真的被这个问题困扰了很多天。在我的应用程序中,我需要在tumblr上上传图像,我已经尝试了各种教程和更新,但是没有一个可以在tumblr上发布图像。如果您已经这样做了,请帮助我在iPhone的tumblr上上传图像,iphone,image,sdk,sharing,Iphone,Image,Sdk,Sharing,我真的被这个问题困扰了很多天。在我的应用程序中,我需要在tumblr上上传图像,我已经尝试了各种教程和更新,但是没有一个可以在tumblr上发布图像。如果您已经这样做了,请帮助我 NSData *imageData = [NSData dataWithContentsOfFile:photo]; //stop on error if (!imageData) return NO; //Create dictionary of post arguments NSArray *keys = [NS
NSData *imageData = [NSData dataWithContentsOfFile: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
NSURLConnection *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://api.tumblr.com/v2/blog/kashifjilani.tumblr.com/posts"];
NSMutableURLRequest *tumblrPost = [NSMutableURLRequest requestWithURL:tumblrURL];
[tumblrPost setHTTPMethod:@"POST"];
//Add the header info
NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
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:[@"Content-Disposition: form-data; name=\"data\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"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;
}
这对我很有用:
NSData *imageData = UIImageJPEGRepresentation(yourUploadImage, 0.9);
NSMutableURLRequest *aRequest = [[[NSMutableURLRequest alloc] init] autorelease];
[aRequest setURL:[NSURL URLWithString:@"https://www.tumblr.com/api/write"]];
[aRequest setHTTPMethod:@"POST"];
NSString *boundary = @"0xKhTmLbOuNdArY";
//NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[aRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"email\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:Tumblr_UserName_Here dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"password\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:Tumblr_Password_Here dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"type\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"photo" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"data\"; filename=\"upload.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Transfer-Encoding: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
if(comment available here)
{
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary]
dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"caption\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[commentString dataUsingEncoding:NSUTF8StringEncoding]];
}
// setting the body of the post to the reqeust
[aRequest setHTTPBody:body];
[NSURLConnection connectionWithRequest:aRequest delegate:self];
现在是NSURLConnection的委托
我也为此挣扎了很长一段时间,但我知道如何轻松发布。
你可以看看我的答案。如果您对此有任何问题,我很乐意提供帮助。向我们展示您的一些代码或您遇到了哪些错误?我正在尝试将代码发布到它上,但不起作用。您的意思是无法将代码发布到stackoverflow上?确保你的代码简洁,并告诉我们你在哪里出错。我已经编辑了我的问题并发布了代码,在控制台中显示提交的请求,但没有任何图片上传到tumblrDid上。你检查我的更新代码吗?嘿,Prince,谢谢你提供的宝贵信息,我已经尝试了您提供的上述步骤,但是仍然不起作用,这表明我在NSLog中成功,但是不起作用,或者将任何内容发布给tumblruse tumblr中经过身份验证的用户,因为tumblr_用户名和密码应该是正确的,然后您将能够为tumblr使用正确的用户名和密码,还有一件事,你说ifcomment在这里可用是什么意思,如果你想在图像中添加注释,比如ifCommentAvailable{post comment}UIImage*imagePassed=[UIImage ImageName:@picture1.jpg];NSData*imageData=UIImagePNGRepresentationimagePassed;使用kashifjilani作为tumblr用户名帐户中的用户名,图像位于资源文件夹中
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if(connection)
NSLog(@"Success");
else
NSLog(@"Something Wrong");
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
{
NSLog(@"%@",[error description]);
}