【发布时间】:2014-05-30 09:24:20
【问题描述】:
我有一个 iOS 应用程序,它向包括 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 希望它是:
source="图像数据...."
此外,如果您查看 URL,您会看到有一个 'message=' 参数。这一切都有效,但我肯定可以将它添加到 HTTPBody 中吗??
我需要使用类似 NSDictionary 的东西吗?
感谢您的宝贵时间, 丹尼尔
【问题讨论】:
标签: ios image cocoa-touch post nsdata