我需要将图像从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对象中获取图像,你觉得怎么样?
答案 0 :(得分:0)
您是否正在实施允许您阅读请求响应的NSURLConnection
委托方法?如果是这样,您应该使用NSMutableData
来累积从服务器返回的数据(可能会在多个委托调用中分割),然后使用UIImage
方法,例如imageWithData:
在客户端上创建图像。例如:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.receivedImageData = [NSMutableData dataWithCapacity:[response expectedContentLength]];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.receivedImageData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
UIImage *finishedImage = [UIImage imageWithData:self.receivedImageData];
self.receivedImageData = nil;
}