【问题标题】:How to upload image to remote server in iphone?如何将图像上传到 iPhone 中的远程服务器?
【发布时间】:2010-03-11 16:30:17
【问题描述】:

我正在尝试上传我在相机的帮助下单击的图像。我正在尝试使用以下代码将图像上传到远程服务器。

-(void)searchAction:(UIImage*)theImage
{
    UIDevice *dev = [UIDevice currentDevice];
    NSString *uniqueId = dev.uniqueIdentifier;

    NSData * imageData = UIImagePNGRepresentation(theImage);

    NSString *postLength = [NSString stringWithFormat:@"%d",[imageData length]];

    NSString *urlString = [@"http://www.amolconsultants.com/im.jsp?" stringByAppendingString:@"imagedata=iPhoneV0&mcid="];
    urlString = [urlString stringByAppendingString:uniqueId];
    urlString = [urlString stringByAppendingString:@"&lang=en_US.UTF-8"];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:imageData];

    NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (conn == nil) 
    {
        NSLog(@"Failed to create the connection");
    }
}

但是什么都没有发布。控制台窗口中也没有任何内容。我在操作表中调用此方法。当用户单击操作表的第一个按钮时,会调用此方法来发布图像。

谁能帮帮我...

任何代码都会很有帮助...

提前谢谢...

【问题讨论】:

  • 我没有立即看到您的代码有任何问题,无论如何您应该在控制台窗口中看到输出。您是否尝试过使用调试器单步执行?
  • 不,我没有尝试调试程序...

标签: iphone objective-c remote-server image-upload


【解决方案1】:

您可以使用名为asi-http-request 的第三方库。它为您简化了大部分艰苦的工作。在您的情况下,您只需执行以下操作:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:imageData];
[request setRequestMethod:@"PUT"];

【讨论】:

  • 但我正在尝试从 iphone 上传图像。该图像不在我的 Mac 中,用户将使用 iPhone 相机点击它,我想将此图像发布到服务器。你能帮我解决这个...
  • 我在这里说的是iphone,不是mac。您可以将图像保存到 Documents 文件夹中,然后执行相同操作。
  • 使用NSData的如下函数:[urlData writeToFile:filePath atomically:NO];
  • 否则,你不需要保存到documents文件夹,这里已经有了nsdata,使用可以使用ASI库的其他功能直接发布NSData。
  • 它给了我错误,说明 ASIHTTPRequest 未在此方法中首次使用。我是否必须导入一些头文件或添加一些框架...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-01
  • 1970-01-01
  • 2016-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多