我需要将图像从 iphone 应用程序上传到 PHP 服务器,以便在服务器上对该图像进行一些处理,然后在服务器完成处理后,我想将结果图像返回给 iphone 应用程序。
我已经有上传代码,但我无法从 php 服务器获取结果图像并将其显示在 iphone 应用程序中
请帮忙
这些是这里的代码:
上传的objective-c代码:
NSString *urlString =[NSString stringWithFormat:@"http://192.168.1.3/TEST%20IPHONE/upload.php"] ;
NSURL *nsurl =[NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsurl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setURL:nsurl];
[request setHTTPMethod:@"POST"];
self.editingImageView.image = nil;
NSString * filename = @"cartoonize.png";
NSMutableData *body = [NSMutableData data];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
// file
NSData *imageData = UIImageJPEGRepresentation(self.EditingImage, 90);
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: attachment; name=\"userfile\"; filename=\"%@\"\r\n",filename]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
//return and test
NSURLConnection * connection = [[NSURLConnection alloc]
initWithRequest:request
delegate:self startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
[connection start];
这里的PHP代码:
<?php
//echo $_FILES["userfile"]["size"];
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["userfile"]["name"]));
if ( ($_FILES["userfile"]["size"] < 200000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["userfile"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["userfile"]["error"] . "<br />";
}
else
{
move_uploaded_file($_FILES["userfile"]["tmp_name"],
"upload/" . $_FILES["userfile"]["name"]);
// some processing on the image and result image will be saved on this path upload/test.jpg
$file = "upload/test.JPG" ;
$imageData = utf8_encode(file_get_contents($file));
echo $imageData;
}
}
else
{
echo "Invalid file";
}
?>
我从 NSUrlConnection 委托方法中获取数据,所以结果在我的 Objective-C 代码中的 NSData 对象中,但是我无法从这个 NSData 对象中获取图像,你们觉得呢?