I need to upload image from iphone application to PHP server for some processing on this image on server, then after the server is finished processing , i want to return the result image to the iphone application.
i already have the code for uploading, but i can’t get the result image from php server and display it within iphone application
Please help
These are the codes here:
The objective-c code for uploading:
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];
Here the PHP code:
<?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";
}
?>
i get the data from NSUrlConnection delegate methods, so the result is in NSData object in my objective-c code, but i can’t get the image from this NSData object, What do you think guys?
Are you implementing the
NSURLConnectiondelegate methods that allow you to read the response of your request? If so, you should use anNSMutableDatato accumulate the data being returned from your server (which is likely to be split across multiple delegate calls), then use aUIImagemethod such asimageWithData:to create the image on the client. For example: