如何将图像保存在我的服务器中
How to save the image in my server
我正在上传从相机或画廊拍摄的图像。
我成功获取了图像,当我上传它时,我在模拟器中遇到错误 User_file=(null).
我的更新码是
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
NSData *dataItems=UIImageJPEGRepresentation(image, 0.1);
NSString *convertimage=[[NSString alloc]initWithData:dataItems encoding:NSUTF8StringEncoding];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@"http:my url"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
//
// NSString *boundary = @"---------------------------14737809831466499882746641449";
// NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
// [urlRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
//
// NSMutableData *body = [NSMutableData data];
//
// [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//
// [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_file\"; filename=\"%d\"\r\n", 1]] dataUsingEncoding:NSUTF8StringEncoding]];
//
// [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//
// [body appendData:[NSData dataWithData:dataItems]];
//
// [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//
// [urlRequest setHTTPBody:body];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *UserID = [prefs stringForKey:@"sfffehr_id"];
NSLog(@"%@",UserID);
NSString *params=[[NSString alloc]initWithFormat:@"sfffehr_id =%@ & user_file=%@",UserID,convertimage];
NSLog(@"%@",params);
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"Response:%@ %@\n", response, error);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
}
}];
[dataTask resume];
我的回复是
fdg[1816:949041] user_file=(null)
fdg[1816:949041] Logo Changed
fdg[1816:949041] Response:<NSHTTPURLResponse: 0x136a2bcb0> { URL: url } { status code: 200, headers {
fdg[1816:949041] 数据 = {"response":"yes","success":"User profile picture updated successfully"}
当我检查我的服务器时,我没有找到我保存的图像。
如何将图片保存到我的服务器,请帮帮我。
我试了很多方法都无济于事
谢谢。
我已对您的代码进行了一些更正。现在试试这个:
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
NSData *dataItems=UIImageJPEGRepresentation(image, 0.1);
NSString *convertimage = [[NSString alloc] initWithData:dataItems encoding:NSUTF8StringEncoding];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@"http://469.5350.747.4557/~lcall/index.php/webservices/update_profile_picture"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[urlRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *UserID = [prefs stringForKey:@"user_id"];
//Populate a dictionary with all the regular values you would like to send.
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:UserID forKey:@"user_id"];
[parameters setValue:convertimage forKey:@"user_file"];
// add params (all params are strings)
for (NSString *param in parameters) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_file\"; filename=\"image.png\"\r\n"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:dataItems]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:body];
// getting an NSString
NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"Response:%@ %@\n", response, error);
if(error == nil){
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
}
}];
[dataTask resume];
注:
我已经对您的代码进行了必要的更正并且没有错误。但我还没有测试过。请检查。
希望对你有用。
我正在上传从相机或画廊拍摄的图像。
我成功获取了图像,当我上传它时,我在模拟器中遇到错误 User_file=(null).
我的更新码是
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
NSData *dataItems=UIImageJPEGRepresentation(image, 0.1);
NSString *convertimage=[[NSString alloc]initWithData:dataItems encoding:NSUTF8StringEncoding];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@"http:my url"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
//
// NSString *boundary = @"---------------------------14737809831466499882746641449";
// NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
// [urlRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
//
// NSMutableData *body = [NSMutableData data];
//
// [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//
// [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_file\"; filename=\"%d\"\r\n", 1]] dataUsingEncoding:NSUTF8StringEncoding]];
//
// [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//
// [body appendData:[NSData dataWithData:dataItems]];
//
// [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//
// [urlRequest setHTTPBody:body];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *UserID = [prefs stringForKey:@"sfffehr_id"];
NSLog(@"%@",UserID);
NSString *params=[[NSString alloc]initWithFormat:@"sfffehr_id =%@ & user_file=%@",UserID,convertimage];
NSLog(@"%@",params);
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"Response:%@ %@\n", response, error);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
}
}];
[dataTask resume];
我的回复是
fdg[1816:949041] user_file=(null)
fdg[1816:949041] Logo Changed
fdg[1816:949041] Response:<NSHTTPURLResponse: 0x136a2bcb0> { URL: url } { status code: 200, headers {
fdg[1816:949041] 数据 = {"response":"yes","success":"User profile picture updated successfully"}
当我检查我的服务器时,我没有找到我保存的图像。
如何将图片保存到我的服务器,请帮帮我。
我试了很多方法都无济于事
谢谢。
我已对您的代码进行了一些更正。现在试试这个:
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
NSData *dataItems=UIImageJPEGRepresentation(image, 0.1);
NSString *convertimage = [[NSString alloc] initWithData:dataItems encoding:NSUTF8StringEncoding];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@"http://469.5350.747.4557/~lcall/index.php/webservices/update_profile_picture"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[urlRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *UserID = [prefs stringForKey:@"user_id"];
//Populate a dictionary with all the regular values you would like to send.
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:UserID forKey:@"user_id"];
[parameters setValue:convertimage forKey:@"user_file"];
// add params (all params are strings)
for (NSString *param in parameters) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_file\"; filename=\"image.png\"\r\n"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:dataItems]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:body];
// getting an NSString
NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"Response:%@ %@\n", response, error);
if(error == nil){
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
}
}];
[dataTask resume];
注: 我已经对您的代码进行了必要的更正并且没有错误。但我还没有测试过。请检查。
希望对你有用。