0

我想从 iOS 将图像上传到网络服务。在 web 服务图像参数数据类型是File. 我想发送用户从他的手机摄像头拍摄的图像的拇指。任何人都可以说如何将拇指图像从 iOS 应用程序发送到网络服务。

谢谢。

4

2 回答 2

4

AFNetworking你可以通过以下方式做到这一点:

NSURL *url = [NSURL URLWithString:@"my_base_url"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"yourImage.jpg"], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name:@"MainMedia" fileName:@"MainMedia" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];

[operation start];
于 2013-06-10T03:23:41.847 回答
2

如果您正在使用ASIHttpRequest库,请查看以下代码:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:yourRequestURL]];

[request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"]; 
[request addRequestHeader:@"Content-Type" value:@"application/json"];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,     NSUserDomainMask, YES);
NSString *savedImagePath = [NSString stringWithFormat:@"%@/%@/%@",[paths objectAtIndex:0],folder,imageName];

if ([[NSFileManager defaultManager] fileExistsAtPath:savedImagePath])
    [request setFile:savedImagePath forKey:@"imgfile"];
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request setDidFailSelector:@selector(requestFailed:)];

我希望它会帮助你。

于 2013-06-10T03:15:24.803 回答