4

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

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

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

谁能帮我这个...

任何代码都会非常有帮助......

提前谢谢...

4

1 回答 1

3

You can use the libraries from a third party called asi-http-request. It simplyfies for you most of the hard works. In your case, you just do as the following:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:imageData];
[request setRequestMethod:@"PUT"];
于 2010-03-11T16:53:39.023 回答