我有一个iOS应用程序,可以在线提供经过身份验证的POST请求,包括Facebook。我的问题是我知道如何POST文本,但我无法发布图片。这是我的代码:
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希望它成为:
源= “图象 - ......”
如果你看一下URL,你会看到有一个'message ='参数。这一切都有效,但我肯定可以将其添加到HTTPBody ??
我需要使用类似NSDictionary的东西吗?
感谢您的时间, 丹尼尔
答案 0 :(得分:0)
感谢开发者社区的帮助lol ....无论如何,如果有人正在阅读本文并且有兴趣知道如何将图像发布到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);
}