Objective c 发布带有参数的图像-AF3.1和目标c(Xcode 8)
我正在寻找一些导致我的问题的线索。 实际上,我已经浏览了所有与向服务器发布/上传图像相关的链接,但似乎没有任何东西可以让我的代码正常工作 我已经发布了2个选项。。。以不同的方式。。。注释的代码表明。。。我的请求在服务器上变成空白 我尝试过使用不同的选项-appendPartWithFormData、appendPartWithFileData 选择1 n错误*错误Objective c 发布带有参数的图像-AF3.1和目标c(Xcode 8),objective-c,image-upload,afnetworking-3,xcode8.1,Objective C,Image Upload,Afnetworking 3,Xcode8.1,我正在寻找一些导致我的问题的线索。 实际上,我已经浏览了所有与向服务器发布/上传图像相关的链接,但似乎没有任何东西可以让我的代码正常工作 我已经发布了2个选项。。。以不同的方式。。。注释的代码表明。。。我的请求在服务器上变成空白 我尝试过使用不同的选项-appendPartWithFormData、appendPartWithFileData 选择1 n错误*错误 // Create paths to output images NSString *pngPath = [NSH
// Create paths to output images
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];
UIImage *image = [UIImage imageWithData:self.imageData];
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
NSURL *imagePath = [NSURL fileURLWithPath:pngPath];
NSData *jsonInputData = [NSJSONSerialization dataWithJSONObject:[requestParameter valueForKey:@"Param"] options:0 error:&error];
NSString *jsonInputString = [[NSString alloc] initWithData:jsonInputData encoding:NSUTF8StringEncoding];
NSLog(@"converted JOSN Data %@",jsonInputString);
NSDictionary* params = @{@"request": jsonInputString };
[manager POST:[requestParameter valueForKey:@"methodName"] parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
//[formData appendPartWithFormData:self.imageData name:@"profilepic" ];
//[formData appendPartWithFileData:self.imageData name:@"profilepic" fileName:@"profilepic.png" mimeType:@"image/png"];
[formData appendPartWithFormData:jsonInputData name:@"request"];
[formData appendPartWithFileURL:[NSURL fileURLWithPath:pngPath]
name:@"File"
error:nil];
} progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"Success: %@", responseObject);
[self requestSuccessed:responseObject];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"Error: %@", error);
[self requestFailed:error];
}];
试试这个,希望它能解决你的问题
// your parametes here
userInfoDict=[[NSMutableDictionary alloc] init];
[userInfoDict setValue:@"image" forKey:@"iamge"];
// if image is too large than the timeout intervel
// [_request setTimeoutInterval:1000];
_request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"api url" parameters:userInfoDict constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
//convert imahe into data
NSData* data = UIImagePNGRepresentation(yourImage);
[formData appendPartWithFileData:data name:@"api paramets name in which you want to post image" fileName:@"myImage.png" mimeType:@"image/png"];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
_uploadTask = [manager
uploadTaskWithStreamedRequest:_request
progress:^(NSProgress * _Nonnull uploadProgress)
{
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
dispatch_async(dispatch_get_main_queue(), ^{
//Update the progress view
[_playerProgress setProgress:uploadProgress.fractionCompleted];
NSLog(@"\n uploading image vlaues %f\n",uploadProgress.fractionCompleted);
// for uploading video in background
manager.attemptsToRecreateUploadTasksForBackgroundSessions=YES;
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
// [_playerProgress setHidden:YES];
} else
{
NSLog(@"%@ ", response);
// after upload comnlete disablle background uploading
manager.attemptsToRecreateUploadTasksForBackgroundSessions=NO;
NSLog(@"\n\n REsponse Form Server %@\n\n",responseObject);
//[responseObject valueForKey:@"error"];
//remove temp file
NSError *error;
}
}
];
[_uploadTask resume];
//这里是您的参数
userInfoDict=[[NSMutableDictionary alloc]init];
[userInfoDict setValue:@“image”forKey:@“iamge”];
//如果映像太大,超过超时间隔
//[\u请求setTimeoutInterval:1000];
_request=[[AFHTTPRequestSerializer序列化程序]multipartFormRequestWithMethod:@“POST”URLString:@“api url”参数:userInfoDict constructingBodyWithBlock:^(id formData)
{
//将imahe转换为数据
NSData*data=UIIMAGEPNGRE演示文稿(yourImage);
[formData appendPartWithFileData:数据名称:@“要发布图像的api参数名称”文件名:@“myImage.png”mimeType:@“image/png”];
}错误:无];
AFURLSessionManager*manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
_uploadTask=[manager]
uploadTaskWithStreamedRequest:\u请求
进度:^(NSProgress*_非空上传进度)
{
//这不会在主队列上回调。
//您负责将UI更新发送到主队列
dispatch\u async(dispatch\u get\u main\u queue()^{
//更新进度视图
[\u playerProgress setProgress:uploadProgress.fractionCompleted];
NSLog(@“\n上载映像文件%f\n”,上载进度。分形已完成);
//用于在后台上传视频
manager.attemptsToRecreateUploadTasksForBackgroundSessions=YES;
});
}
completionHandler:^(NSURLResponse*\u非空响应,id\u可空响应对象,NSError*\u可空错误){
如果(错误){
NSLog(@“错误:%@”,错误);
//[[u playerProgress setHidden:是];
}否则
{
NSLog(@“%@”,响应);
//上传后comnlete可禁用后台上传
manager.attemptsToRecreateUploadTasksForBackgroundSessions=否;
NSLog(@“\n\n响应表单服务器%@\n\n”,响应对象);
//[响应对象值forkey:@“错误”];
//删除临时文件
n错误*错误;
}
}
];
[_上传任务恢复];
试试这个,希望它能解决你的问题
// your parametes here
userInfoDict=[[NSMutableDictionary alloc] init];
[userInfoDict setValue:@"image" forKey:@"iamge"];
// if image is too large than the timeout intervel
// [_request setTimeoutInterval:1000];
_request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"api url" parameters:userInfoDict constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
//convert imahe into data
NSData* data = UIImagePNGRepresentation(yourImage);
[formData appendPartWithFileData:data name:@"api paramets name in which you want to post image" fileName:@"myImage.png" mimeType:@"image/png"];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
_uploadTask = [manager
uploadTaskWithStreamedRequest:_request
progress:^(NSProgress * _Nonnull uploadProgress)
{
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
dispatch_async(dispatch_get_main_queue(), ^{
//Update the progress view
[_playerProgress setProgress:uploadProgress.fractionCompleted];
NSLog(@"\n uploading image vlaues %f\n",uploadProgress.fractionCompleted);
// for uploading video in background
manager.attemptsToRecreateUploadTasksForBackgroundSessions=YES;
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
// [_playerProgress setHidden:YES];
} else
{
NSLog(@"%@ ", response);
// after upload comnlete disablle background uploading
manager.attemptsToRecreateUploadTasksForBackgroundSessions=NO;
NSLog(@"\n\n REsponse Form Server %@\n\n",responseObject);
//[responseObject valueForKey:@"error"];
//remove temp file
NSError *error;
}
}
];
[_uploadTask resume];
//这里是您的参数
userInfoDict=[[NSMutableDictionary alloc]init];
[userInfoDict setValue:@“image”forKey:@“iamge”];
//如果映像太大,超过超时间隔
//[\u请求setTimeoutInterval:1000];
_request=[[AFHTTPRequestSerializer序列化程序]multipartFormRequestWithMethod:@“POST”URLString:@“api url”参数:userInfoDict constructingBodyWithBlock:^(id formData)
{
//将imahe转换为数据
NSData*data=UIIMAGEPNGRE演示文稿(yourImage);
[formData appendPartWithFileData:数据名称:@“要发布图像的api参数名称”文件名:@“myImage.png”mimeType:@“image/png”];
}错误:无];
AFURLSessionManager*manager=[[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
_uploadTask=[manager]
uploadTaskWithStreamedRequest:\u请求
进度:^(NSProgress*_非空上传进度)
{
//这不会在主队列上回调。
//您负责将UI更新发送到主队列
dispatch\u async(dispatch\u get\u main\u queue()^{
//更新进度视图
[\u playerProgress setProgress:uploadProgress.fractionCompleted];
NSLog(@“\n上载映像文件%f\n”,上载进度。分形已完成);
//用于在后台上传视频
manager.attemptsToRecreateUploadTasksForBackgroundSessions=YES;
});
}
completionHandler:^(NSURLResponse*\u非空响应,id\u可空响应对象,NSError*\u可空错误){
如果(错误){
NSLog(@“错误:%@”,错误);
//[[u playerProgress setHidden:是];
}否则
{
NSLog(@“%@”,响应);
//上传后comnlete可禁用后台上传
manager.attemptsToRecreateUploadTasksForBackgroundSessions=否;
NSLog(@“\n\n响应表单服务器%@\n\n”,响应对象);
//[响应对象值forkey:@“错误”];
//删除临时文件
n错误*错误;
}
}
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSLog(@"Saving image file");
[formData appendPartWithFileData:UIImagePNGRepresentation(image) name:@"imagename" fileName:@"image.png" mimeType:@"image/png"];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"JSON uploadProgress.fractionCompleted: %f", uploadProgress.fractionCompleted);
});
} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
//NSLog(@"Error: %@", error);
NSLog(@"RESPONSE: %@ %@", response, responseObject);
if(error) {
NSLog(@"Error: %@", error);
} else {
NSString *responseString = [[[NSString alloc] initWithData:responseObject encoding:NSASCIIStringEncoding] mutableCopy];
NSLog(@"RESPONSE: %@", responseString);
}
}];
[uploadTask resume];