【问题标题】:How to send a image from iOS to web service [duplicate]如何将图像从 iOS 发送到 Web 服务 [重复]
【发布时间】:2013-06-05 15:54:15
【问题描述】:

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

谢谢。

【问题讨论】:

  • 我想它会涉及一个 POST 请求。如果没有一个更具体的问题,没有人能够更详细。
  • 我想知道使用POST请求上传图片的方式。
  • 我已经把它转换成base64格式了,,,但是从那以后我应该怎么做
  • @iDia 你看过上面问题的答案了吗?它似乎正是你想要的。
  • 关于这个主题已经有一个重复的问题stackoverflow.com/questions/5422028/… 你可能会发现这个链接也很有帮助。 insanelycrazy.net/development/faces/viewarticle.xhtml?id=6

标签: ios image web-services


【解决方案1】:

使用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];

【讨论】:

    【解决方案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:)];
    

    希望对你有帮助。

    【讨论】:

    • 您也可以通过 AFNetwork 库来实现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多