我正在开发一个iPhone应用程序,其中NSData
到UIImageJPEGRepresentation
中的相机图像存储现在我想将此NSData转换为字符串。在goggling之后我发现base64会这样做。
我想知道,如果我将它编码到base64然后将其发送到php服务器,那么就可以对其进行解码并转换为Image。
我首先问here如何将带有坐标和图像描述的图像发送到php服务器但我无法得到响应,所以现在我想将图像转换为base64,然后将其发送到带有坐标和图像描述的php服务器
希望有人知道解决方案!
答案 0 :(得分:0)
Base64是一种标准,并且受到包括PHP在内的大多数语言的支持,所以它可以工作。 但这可能不是最好的方法,因为base64将有效负载大小增加了33%。您应该考虑将其作为二进制而不是字符串发送,这也将更好地支持流式传输,并且将使用更少的内存并在客户端和服务器上更快地工作。 你应该只在小数据上使用base64,或者没有更好的选择。
答案 1 :(得分:0)
查看以下网址。它可能会给你你想要的东西。
答案 2 :(得分: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"];
答案 3 :(得分: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];
}