iPhone SDK中如何将NSData转换为字符串?

发布于 2024-11-01 22:08:48 字数 452 浏览 3 评论 0原文

我正在开发一个 iPhone 应用程序,其中相机图像通过 UIImageJPEGRepresentation 存储在 NSData 中,现在我想将此 NSData 转换为字符串。经过一番研究,我发现 base64 可以做到这一点。

我想知道,如果我将其编码为base64,然后将其发送到php服务器,那么就可以对其进行解码并转换为图像。

我首先在这里询问这里 如何将带有坐标和图像描述的图像发送到php服务器,但我无法得到响应,所以现在我想将图像转换为base64,然后将其发送到带有坐标和图像描述的php服务器。

希望有人知道解决办法!

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 image 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 !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一枫情书 2024-11-08 22:08:48

这对我来说听起来不太好。一些 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"];

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"];
客…行舟 2024-11-08 22:08:48

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

浅紫色的梦幻 2024-11-08 22:08:48

Base64 是一种标准,并且受到包括 PHP 在内的大多数语言的支持,因此它可以工作。
但这可能不是最好的方法,因为 base64 将有效负载大小增加了 33%。您应该考虑将其作为二进制而不是字符串发送,这也将更好地支持流式传输,并且将使用更少的内存并且在客户端和服务器上工作得更快。
您应该仅在小数据或没有更好的选择时使用 base64。

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.

甜心小果奶 2024-11-08 22:08:48

按照此代码将图像和变量发送到服务器 -

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



}

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



}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文