嗨,我是ios的新手,我正在尝试使用NSURLSession将一些参数发送到服务器(它们是USERNAME,USERTYPE,USERimage)
但根据我下面的代码图像不会发送到服务器,以及如何将图像发送到服务器,请帮助我我在这里哪里做错了?
我的代码:
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"[JSON SERVER"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
UIImage *myImage = [UIImage imageNamed:@"myImage.png"];
NSString *myImage = [UIImagePNGRepresentation(myImage) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"TEST IOS", @"name",
@"IOS TYPE", @"typemap",
myImage,@"image"
nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
[request setHTTPBody:postData];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}];
[postDataTask resume];
最佳答案
尝试使用
UIImage *myImage = [UIImage imageNamed:@"myImage.png"];
NSString *base64Image = [UIImagePNGRepresentation(myImage) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];