上传图片时出现问题

时间:2015-05-15 05:08:16

标签: ios objective-c afnetworking image-uploading nsurlsessionuploadtask

第1步:这里我正在创建请求

NSMutableURLRequest *request1 = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST"
                                                                                           URLString:[NSString stringWithFormat:@"%@%@", API_MAIN_URL, IMAGE_UPLOAD]
                                                                                          parameters:param constructingBodyWithBlock:^(id formData) {

           [formData appendPartWithFileURL:[NSURL fileURLWithPath:strImagePath] 
                                      name:@"sendimage" 
                                  fileName:[strImagePath lastPathComponent] 
                                  mimeType:@"image/png"
                                     error:nil];
                                       } error:nil];
[request1 setValue:authToken forHTTPHeaderField:@"Authorization"];

第2步:此处我在给定位置创建了流

[[AFHTTPRequestSerializer serializer] requestWithMultipartFormRequest:request1 
                                          writingStreamContentsToFile:[NSURL fileURLWithPath:[strImagePath stringByDeletingPathExtension]] 
                                                    completionHandler:^(NSError *error){

第3步:我在这里创建上传任务。

        ///here is file
        NSProgress *progress = nil;
        NSURLSessionUploadTask *uploadTask = [self.manager uploadTaskWithRequest:request1
                                                                        fromFile:[NSURL fileURLWithPath:strImagePath]
                                                                        progress:&progress
                                                               completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
                                                                       NSLog(@"response : %@\n\n responseObject : %@\n\n error : %@", response, responseObject, error);

                                                               }];
        [uploadTask resume];
    }
                                                    }];
}

我的问题是在应用程序进入后台模式之前我想在给定位置写入所有请求HTTPBody(HTTPStream),使用Step:1和Step:2并将所有请求保存到{{1}在文件中写入所有NSArray之后(在应用程序前景中)意味着我将显示图像准备上传。

然后我将在Step3的帮助下开始创建后台任务:将请求传递给我已存储到HTTPStream的请求。 使用这种方法,我无法上传图像。

但是如果我一个接一个地调用所有步骤,那么它会将图像上传到服务器上,但是使用这种方法,我的应用程序应该在Foreground中用于创建请求,因为我们需要写NSArray给出位置。

请帮助我解决这个问题我从最近2周就坚持了这个问题。 我的应用程序需要通过服务器上传超过500张图像。

2 个答案:

答案 0 :(得分:0)

试试这个

 NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
            } error:nil];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        NSProgress *progress = nil;

        NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            if (error) {
                NSLog(@"Error: %@", error);
            } else {
                NSLog(@"%@ %@", response, responseObject);
            }
        }];

        [uploadTask resume];

答案 1 :(得分:0)

我已完成此任务,创建所有上传任务而不恢复它们并在创建后保存NSArray中的每个任务。 创建完所有任务后,调用我们恢复所有任务的方法。

相关问题