我需要使用两个网址发送两个图像。这个障碍让我感到困难,我需要从第一个网址获得响应,我需要将它与第二个网址一起使用。
NSString *requestString =[NSString stringWithFormat:@"UserId=%@&CategoryId=%@&Continent=%@&Country=%@&City=%@&Gender=%@&ImageName=%@&AgeRange=%@",PassedUserId,CategoryId,continentTextfield.text,countrytextfield.text,citytextfield.text,GenderText.text,imagename,ageTextfield.text];
NSLog(@"%@",requestString);
NSString *url=[NSString stringWithFormat:@"http://192.168.2.4:98/InsertObjectImage?%@",requestString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
// Create 'POST' MutableRequest with Data and Other Image Attachment.
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
NSData *data = UIImageJPEGRepresentation(chosenImage, 0.2f);
[request addValue:@"image/JPEG" forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:data]];
[request setHTTPBody:body];
NSData *returnData;
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Ret: %@",returnString);
NSLog(@"imageid%@",compareId);
这里我需要获取imageId并且我将与第二个URL一起使用。
NSString *requestString1 =[NSString stringWithFormat:@"UserId=%@&ImageName=%@&ImageId=%@",PassedUserId,imagename,compareId];
NSLog(@"secondImage%@",requestString1);
NSString *url=[NSString stringWithFormat:@"http://192.168.2.4:98/UserImage.svc/UpdateObjectImage??%@",requestString1];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
// Create 'POST' MutableRequest with Data and Other Image Attachment.
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
NSData *data = UIImageJPEGRepresentation(chosenImage, 0.2f);
[request addValue:@"image/JPEG" forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:data]];
[request setHTTPBody:body];
NSData *returnData;
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Ret: %@",returnString);
答案 0 :(得分:0)
为什么不为上传图像创建单独的类。像
这样的东西 ImageUploader.h/m
在类中实现所有功能,并调用函数以在不同的线程中上传图像。见下文
<强> ImageUploader.h
强>
#import <Foundation/Foundation.h>
@interface ImageUploader : NSObject
-(void)uploadImage:(NSData *)imageData withRequestURL:(NSString *)url;
@end
<强> ImageUploader.m
强>
#import "ImageUploader.h"
@implementation ImageUploader
-(void)uploadImage:(NSData *)imageData withRequestURL:(NSString *)url{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
// Create 'POST' MutableRequest with Data and Other Image Attachment.
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
[request addValue:@"image/JPEG" forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:imageData];
[request setHTTPBody:body];
NSData *returnData;
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"Ret: %@",returnString);
NSLog(@"imageid%@",compareId);
}
@end
然后调用函数
第一次请求
NSString *requestString =[NSString stringWithFormat:@"UserId=%@&CategoryId=%@&Continent=%@&Country=%@&City=%@&Gender=%@&ImageName=%@&AgeRange=%@",PassedUserId,CategoryId,continentTextfield.text,countrytextfield.text,citytextfield.text,GenderText.text,imagename,ageTextfield.text];
NSString *url=[NSString stringWithFormat:@"http://192.168.2.4:98/InsertObjectImage?%@",requestString];
ImageUploader *uploader1=[[ImageUploader alloc] init];
[uploader1 uploadImage:UIImageJPEGRepresentation(chosenImage, 0.2f) withRequestURL:url];
第二次请求
requestString=[NSString stringWithFormat:@"UserId=%@&ImageName=%@&ImageId=%@",PassedUserId,imagename,compareId];
url=[NSString stringWithFormat:@"http://192.168.2.4:98/UserImage.svc/UpdateObjectImage??%@",requestString];
ImageUploader *uploader2=[[ImageUploader alloc] init];
[uploader2 uploadImage:UIImageJPEGRepresentation(chosenImage, 0.2f) withRequestURL:url];
上述两个函数都将被一起调用。顺便说一句,你应该Asynchronously
,而不是Synchronously
。
干杯。
答案 1 :(得分:0)
使用分组编码
typedef void (^onSuccess)(BOOL success);
//使用属性
@property (nonatomic,retain) onSuccess block;
//拨打电话
[self uploadFileWithURL:@"First URL" withCompletionBlock:^(BOOL success) {
if (success) {
[self uploadFileWithURL:@"Second" withCompletionBlock:nil]
}
}];
//连接
-(void) uploadFileWithURL:(NSURL *)url withCompletionBlock:(onSuccess)completeBlock {
self.block = [completeBlock copy];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:url] delegate:self];
[connection start];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;{
self.block(TRUE);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;{
self.block(FALSE);
}