2012-03-06 51 views
1

首先我告訴我相關的代碼:如何改變的UIImage的NSData的成二進制數據,以發送到PHP

轉換的UIImage到NSData的:

imageData = UIImagePNGRepresentation(myImage); 

然後我寫了NSMutableRequest:

NSString *urlString = @"http://136.206.46.10/~katie_xueke/test.php"; 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init]autorelease]; 
[request setURL:[NSURL URLWithString:urlString]]; 
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; 
[request setHTTPShouldHandleCookies:NO]; 
[request setTimeoutInterval:30.0f]; 

[request setHTTPMethod:@"POST"]; 

然後我寫了NSMutableData:

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
NSMutableData *body = [NSMutableData data]; 

//Image 
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name =\"image\";filename=\"%@\"\r\n",imageName] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Type:application/octet-stream\r\n\r\n"]dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"Content-Length: %@\r\n",postLength]dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:imageData]]; 
[body appendData:[[NSString stringWithFormat:@"--%@--",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

$$的問題是:NSData的似乎無法發送到服務器端和身體部位顯示在控制檯窗口是這樣的:

Content-Disposition: form-data; name ="image";filename="2012:03:06 15:06:48" 

Content-Type:application/octet-stream 



Content-Length: 164692 

‰PNG 

如何改變的UIImage的NSData的成二進制數據,以便發送到PHP ??

PS:我試過UINT8方法

UInt8 *rawData = [imageData bytes]; 

但似乎的iOS 5已經過時了。

在PHP端:

$uploaddir = './upload/';  
echo "recive a image";  
$file = basename($_FILES['userfile']['name']);  
$uploadfile = $uploaddir . $file;  

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {  
    echo "/uploads/{$file}";  

}

我從別的地方抄了一遍,我不知道如何顯示該網頁上我的職務圖像。

有人可以幫助我嗎?

非常感謝。

+0

你沒有正確地將圖像轉換爲數據,但是您要查看的二進制爲字符串。該輸出是PNG標題的一部分,並且由於發生空字符而停止打印。要轉儲數據,您需要使用十六進制。 ('NSLog'一個'NSData'對象將執行十六進制轉儲) – Joe 2012-03-06 15:22:14

+0

你知道如何使用php顯示圖像嗎? – 2012-03-06 15:24:33

+0

'-bytes'肯定不會被棄用。這是班級的原始人!是什麼讓你相信它是? – kperryua 2012-03-06 15:32:28

回答

2

最後我自己想出瞭解決方案。

AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://XXXX"]]; 
NSMutableURLRequest *myRequest = [client multipartFormRequestWithMethod:@"POST" path:@"upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 
    [formData appendPartWithFileData:imageData name:@"uploadedfile" fileName:dateTime mimeType:@"images/png"]; 
}]; 
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:myRequest]; 
[operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) { 

    NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite); 

}]; 
[operation setCompletionBlock:^{ 
    NSLog(@"response string: %@", operation.responseString); //Lets us know the result including failures 
}]; 

NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease]; 
[queue addOperation:operation]; 

我用AFNetworking代替ASIHttpRequest。

在PHP端,我的代碼:

<?php 

$filename="uploaded"; 
$target_path = "uploads/"; 

$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['uploadedfile']['name']). 
    " has been uploaded"; 
} else{ 
    echo "There was an error uploading the file, please try again!"; 
} 
?> 

並感謝你們誰幫我。

PS:非常感謝這個人究竟是誰幫我在這: http://6foot3foot.com/developer-journal/afnetworking-php