【问题标题】:Perform Action with two urls to send two images at a time使用两个 url 执行操作以一次发送两个图像
【发布时间】:2014-08-05 21:01:00
【问题描述】:

我需要使用两个 url 发送两张图片。这里的障碍是我需要从第一个 url 获得响应,我需要将它与第二个 url 一起使用。

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

【问题讨论】:

  • 检查答案只需使用您的请求

标签: ios iphone objective-c image url


【解决方案1】:

使用块编码

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

}

【讨论】:

    【解决方案2】:

    为什么不创建一个单独的类来上传图像。类似的东西

    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

    干杯。

    【讨论】:

    • 您好@iphonic thanx 为您解答,但我需要在同一个 ViewController 中使用
    • @user3357097 我已经更新了答案以支持不同的 url,是的,您可以直接在 ViewController 中使用它,只需 #import ImageUploader.h 并使用 FirstSecond 我写的请求调用
    • @user3357097 你的意思是你需要先调用第一个服务,然后再调用其他服务吗?
    • @user3357097 一个接一个地上传图像是没有意义的,你应该使用队列,但是如果你想一个接一个地上传图像,你当然可以像@sunny 描述的那样接近block 方式.
    猜你喜欢
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多