2011-04-14 55 views
0

我正在開發一個iPhone應用程序,其中相機圖像存儲在NSDataUIImageJPEGRepresentation現在我想將此NSData轉換爲字符串。在觀看後,我發現base64會做到這一點。如何將NSData中的NSData轉換爲字符串?

我想知道,如果我將它編碼到base64,然後將其發送到php服務器,那麼它可以解碼並轉換成圖像。

我第一次問here如何發送帶有座標和圖像描述的圖像到php服務器,但我無法得到響應,所以現在我想將圖像轉換成base64,然後將它發送到具有座標和圖像描述的php服務器。

希望有人知道解決方案!

回答

0

這聽起來不錯。一些http客戶端爲圖片上傳提供了很好的支持。看看ASIHttpRequest。您可以將數據保存到臨時文件,然後上傳該文件。我之前沒有使用addData方法,看起來像你可以直接上傳NSData對象。

 
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
[request addPostValue:@"Ben" forKey:@"names"]; 
[request addPostValue:@"George" forKey:@"names"]; 
[request addFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photos"]; 
[request addData:imageData withFileName:@"george.jpg" andContentType:@"image/jpeg" forKey:@"photos"]; 
0

Base64是一個標準,並受包括PHP在內的大多數語言的支持,所以它可以工作。 但是這可能不是最好的方法,因爲base64將有效載荷大小增加了33%。您應該考慮以二進制形式發送它,而不是字符串,這也會更好地支持流式傳輸,並且會在客戶端和服務器上使用更少的內存並且工作得更快。 您應該只在小數據上使用base64,或者沒有更好的選項。

0

關注該代碼發送圖像和變量服務器 -

// Implement this method in your code to do something with the image. 
- (void)useImage:(UIImage*)theImage 
{ 
    /* 
    turning the image into a NSData object 
    getting the image back out of the UIImageView 
    setting the quality to 90 
    */ 

    NSData *imageData = UIImageJPEGRepresentation(theImage, 90); 
    // setting up the URL to post to 


    NSString *urlString=[SiteUrl stringByAppendingString:@"iphone_upload_photo.php?type=colleague&token="]; 
    urlString=[urlString stringByAppendingString:PublicToken]; 





    // setting up the request object now 
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
    [request setURL:[NSURL URLWithString:urlString]]; 
    [request setHTTPMethod:@"POST"]; 

    /* 
    add some header info now 
    we always need a boundary when we post a file 
    also we need to set the content type 

    You might want to generate a random boundary.. this is just the same 

    */ 
    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 

    /* 
    now lets create the body of the post 
    */ 
    NSMutableData *body = [NSMutableData data]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.jpg\"\r\n"] 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 stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    // setting the body of the post to the reqeust 
    [request setHTTPBody:body]; 

    // now lets make the connection to the web 
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

    [self LoadContent]; 



}