如何将图片发布到Web服务器

4

我正在使用JSON解析处理Web服务。我能够从Web服务获取图像,但是我需要帮助来了解如何发布一张图片。我应该如何将图片发布到Web服务中呢?


你考虑过使用 ASIHTTPRequest 吗? - limon
@mstfbsnli 我是iOS的新手...所以,请帮助我,如何通过ASIHTTPRequest实现它..拜托了。 - Kundan
这里是使用说明的解释 - limon
4个回答

4

这将与以下内容类似...

NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:@"you url"];

[mutableRequest addValue:@"image/jpeg" forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];

[body appendData:[NSData dataWithData:UIImageJPEGRepresentation(self.image, 0.9)]];

[mutableRequest setHTTPBody:body];

NSURLResponse *response;
NSError *error;

(void) [NSURLConnection sendSynchronousRequest:mutableRequest returningResponse:&response error:&error];

谢谢


谢谢…但是我应该在哪个方法中添加这些代码呢? - Kundan
不确定你所说的是哪个方法?这完全取决于你编写程序的方式。在某个时候,你会有一个名为“uploadImage”或类似的方法。好的,这就是你上传该图像的方式。 - Fogmeister
谢谢。我问这个问题是因为我是iOS的新手,对它不是很了解。 - Kundan

2
在此处找到与服务器端(PHP)图片上传相关的随机命名的编码。它还将以响应方式提供图像链接。
//Create a folder named images in your server where you want to upload the image.
// And Create a PHP file and use below code .


<?php
$uploaddir = 'images/';
$ran = rand () ;

$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir .$ran.$file;

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "www.host.com/.../images/{$uploadfile}";
}
?>

以下是iOS代码:

- (IBAction)uploadClicked:(id)sender
{
    /*
         turning the image into a NSData object
         getting the image back out of the UIImageView
         setting the quality to 90
        */
        NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);
        // setting up the URL to post to
        NSString *urlString = @"your URL link";

        // setting up the request object now
        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
        [request setURL:[NSURL URLWithString:urlString]];
        [request setHTTPMethod:@"POST"];

        /*
         add some header info now
         we always need a boundary when we post a file
         also we need to set the content type

         You might want to generate a random boundary.. this is just the same 
         as my output from wireshark on a valid html post
        */
        NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

        /*
         now lets create the body of the post
        */
        NSMutableData *body = [NSMutableData data];
        [body appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
        [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="userfile"; filename="ipodfile.jpg"rn"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-streamrnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:imageData]];
        [body appendData:[[NSString stringWithFormat:@"rn--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        // setting the body of the post to the reqeust
        [request setHTTPBody:body];

        // now lets make the connection to the web
        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

        NSLog(returnString);

}

请查看我的编辑后的答案,让我知道您遇到了什么问题或哪一行您无法理解。 - Siba Prasad Hota
@SibaPrasadHota,您能告诉我是否需要一次性将图像块发送到PHP Web服务器上。那么应该如何做才是最好的呢?我已经将图像数据大小减小到100 KB。谢谢。 - Anand Gautam
@AnandGautam 我已经使用for循环中的相同过程来完成了这个操作,但我不知道这是否是正确的方法。 - Siba Prasad Hota
@SibaPrasadHota你能看到这个链接并给我一些建议吗 - http://stackoverflow.com/questions/20513116/how-to-upload-image-file-from-document-directory-to-php-server-in-iphone/20513229?noredirect=1#comment30666756_20513229 - Anand Gautam
@AnandGautam 请查看您链接中的答案,我尝试了并且有效。 - Siba Prasad Hota
显示剩余3条评论

2

谢谢,但是必须了解服务器端,因为我只有发布的URL,没有服务器端信息。 - Kundan
只需询问服务器端开发人员他已经实现了什么,还要求他提供一个HTML表单来验证他的代码。他的一些问题可能会导致你的噩梦 :) - Susim Samanta
你提到的链接已经很好地描述了它,但是那里的图片无法显示。你有其他链接或代码吗?请帮忙。 - Kundan
你必须使用你的服务器链接对它进行测试。 - Susim Samanta

0

对于新项目,它不再起作用了。http://allseeing-i.com/ASIHTTPRequest/How-to-use - Siba Prasad Hota

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接