2012-08-09 25 views
0

我需要將圖像從iPhone應用程序上傳到PHP服務器,以便在服務器上對此圖像進行一些處理,然後在服務器完成處理後,我要將結果圖像返回給iphone應用程序。上傳和下載圖像到php服務器進行一些處理

我已經有上傳的代碼,但我無法從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

您是否正在實施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; 
} 
+0

是的,我這樣做,但圖像總是零,是什麼問題?它來自服務器圖像編碼嗎? – 2012-08-09 18:00:45

+0

爲什麼你在服務器上處理過的圖像數據上使用'utf8_encode'?爲什麼不直接從'file_get_contents'發送原始八位字節?此外,在響應中爲Content-Type和Content-Length設置標題值並不會造成什麼影響,但它仍然可以在沒有它們的情況下運行。 – warrenm 2012-08-09 18:18:39