使用对象参数发布-iOS
我有一个iOS应用程序,它向包括Facebook在内的在线服务发出经过身份验证的POST请求。我的问题是我知道如何发布文本,但我不能发布图像。这是我的密码:使用对象参数发布-iOS,ios,image,cocoa-touch,post,nsdata,Ios,Image,Cocoa Touch,Post,Nsdata,我有一个iOS应用程序,它向包括Facebook在内的在线服务发出经过身份验证的POST请求。我的问题是我知道如何发布文本,但我不能发布图像。这是我的密码: NSData *imageData = UIImagePNGRepresentation(image_selected); NSString *postLength = [NSString stringWithFormat:@"%d", [imageData length]]; // Init the URLReques
NSData *imageData = UIImagePNGRepresentation(image_selected);
NSString *postLength = [NSString stringWithFormat:@"%d", [imageData length]];
// Init the URLRequest
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
NSString *facebook_picture_url = [NSString stringWithFormat:@"https://graph.facebook.com/ID/photos?message=%@&access_token=%@", inputted_text, token_facebook];
[request setURL:[NSURL URLWithString:facebook_picture_url]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:imageData];
request.HTTPBody = imageData;
request.HTTPMethod = @"POST";
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"The responce:\n\n%@", response);
if (connection) {
// response data of the request
}
如您所见,我可以将“imageData”添加到HTTPBody中,但问题是一些服务希望将其作为某种参数。例如:Facebook希望它是:
source=“imagedata…”
另外,如果您查看URL,您将看到有一个“message=”参数。这一切都是可行的,但我肯定可以将其添加到HTTPBody中
我需要使用类似于词典的东西吗
谢谢你抽出时间,
Daniel感谢开发者社区的帮助,哈哈。。。。无论如何,如果有人正在阅读本文,并且有兴趣知道如何将图像发布到Facebook等服务,我就是这样做的:
NSData *imageData = UIImagePNGRepresentation(image_selected);
NSString *facebook_picture_url = [NSString stringWithFormat:@"https://graph.facebook.com/USER_ID/photos?message=%@&access_token=%@", inputted_text, token_facebook];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:facebook_picture_url]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\".JPG\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"The responce:\n\n%@", responseData);
if (error == nil && response.statusCode == 200) {
NSLog(@"%li", (long)response.statusCode);
}
else {
//Error handling
NSLog(@"%@", response);
}