1

我目前正在将 uiimages 发送到我的服务器以存储在服务器上。所以我从用户图库中获取图像,然后使用 Base64 对其进行编码,然后将其发送到我的服务器。但是,我发送到服务器的所有图像都没有显示。我想知道我使用 base64encodedstring 发出的 post 请求是否会改变编码字符串。

这是我所做的。

//Upload picture to server
NSData *imageData=UIImageJPEGRepresentation(showPic.image, 1.0f);
NSLog(@"The size of the image is %i",[imageData length]);
[Base64 initialize];
NSString *strEncoded = [Base64 encode:imageData];

//NSLog(@"This is the encoded string to be uploaded %@",encodedPic);
prefs=[NSUserDefaults standardUserDefaults];
userid=[prefs stringForKey:@"userid"];

NSLog(@" %@ is the userid to be used to upload the pic",userid);

NSString *poster = @"username=";
poster = [poster stringByAppendingString:userid];
poster = [poster stringByAppendingString:@"&image="];
poster = [poster stringByAppendingString:strEncoded];



NSString *post = poster;
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://www.example.com/server/upload"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
4

0 回答 0