在我的IOS应用程序中,我想使用HTTP请求将图像与图像名称一起发送到服务器 我是一名具有嵌入式背景的程序员,因此不了解HTTP调用,也是iPhone开发的新手。
如何实现这一点,任何示例代码或教程都将受到赞赏。
答案 0 :(得分:2)
更好的方法是首先使用Image Compress Library Here压缩图像,然后使用网络库Liek AF Networking上传它,或者也可以使用NSUrlConnection发送它。 AFNetworking易于使用。您可以访问this page,了解如何将其导入到项目中。写下这些代码行。
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
[manager POST:@"http://samwize.com/api/poo/"
parameters:@{@"color": @"green"}
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
答案 1 :(得分:0)
答案 2 :(得分:0)
为此,您可以使用ASIHTTPRequest
执行此类操作NSURL *strUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@?action=youraction",serverUrl]];
ASIFormDataRequest *request;
request = [[[ASIFormDataRequest alloc] initWithURL:strUrl] autorelease];
[request setRequestMethod:@"POST"];
[request setTimeOutSeconds:120];
NSString *imagePAth = userImagePath;
NSArray *imageName = [userImagePath componentsSeparatedByString:@"/"];
if( userImagePath)
{
[request setFile:imagePAth withFileName:[imageName lastObject] andContentType:@"image/jpeg" forKey:@"profileImage"];
}
[request setUseCookiePersistence:NO];
[request setUseSessionPersistence:NO];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestFinished:)];
[request setDidFailSelector:@selector(requestFailed:)];
[request startAsynchronous];
- (void)requestFinished:(ASIHTTPRequest *)request
{
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
}
答案 3 :(得分:0)
使用NSURLConnection,确保将图像转换为NSData user_id和key有参数。
NSURL *URL = [NSURL URLWithString:constFileUploadURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"0xLhTaLbOkNdArZ";
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", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_id\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *userid = [NSString stringWithFormat:@"%li",userID];
[body appendData:[userid dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[constBackendKey dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
for (NSData *data in arrayWithFiles)
{
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"files%ld\"; filename=\"image%ld.jpg\"\r\n",[arrayWithFiles indexOfObject:data],[arrayWithFiles indexOfObject:data]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
_connection = [NSURLConnection connectionWithRequest:request delegate:self];