图像上传到服务器

时间:2016-07-12 15:08:27

标签: ios objective-c

我想将图像和参数发送到服务器,图像应该是base64格式。

Ex:我的网址:http:// .............

参数:image = image(base64格式),filename =我的图像名称

我尝试使用NSURLSessions,图像发送到服务器但图像字节为ZERO。

我的代码是:

UIImage *dataImage = [UIImage imageWithData:strImageBase64];
NSString *ImageBase64 = [UIImagePNGRepresentation(dataImage) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

NSURL * url = [NSURL URLWithString:myurl];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString * params = [NSString stringWithFormat:@"image=%@&filename=%@",ImageBase64,@"unique2.png"];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];

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

0 个答案:

没有答案