如何将图像上传到iphone中的远程服务器?

时间:2010-03-11 16:30:17

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

我正在尝试上传一张我在相机帮助下点击的图片。我正在尝试以下代码将图像上传到远程服务器。

-(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");
    }
}

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

任何人都可以帮助我...

任何代码都会非常有用......

提前完成了......

1 个答案:

答案 0 :(得分:3)

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

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