开发者

How to convert NSData into string in iPhone SDK?

I am developing an iPhone application in which camera image store in NSData through UIImageJPEGRepresentation now i want to convert this NSData into string. After goggling I found base64 will do this.

I want to know that if i encode it into base64 and then send it to php server, then it is possible to decode it and convert into Image.

I first ask here how to send Image with coordinates and i开发者_开发技巧mage description to php server but i cannot got response, so now i think to convert image into base64 and then send it to php server with coordinates and image description.

Hope someone know the solution !


That doesn't sound good to me. Some http clients provide nice support for image upload. Take a look at ASIHttpRequest. You can save the data to a temp file then upload that file. I didn't use addData method before, looks like you can upload NSData object directly.

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"];


Take a look at the following url. It may give you what you are looking for.

http://www.iphonedevsdk.com/forum/iphone-sdk-development/49247-posting-image-web-server-using-soap.html


Base64 is a standard and is supported by most languages including PHP, so it would work. But this is probably not the best way to do it, as base64 increases the payload size by 33%. You should look into sending it as binary and not string, this will also better support streaming and will use less memory and work faster both on client and server. You should use base64 only on small data or when there are no better options.


follow this code to send images and variables to server -

// 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];



}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜