使用AFNetworking 3.0上传图像

Mar*_* P. 5 iphone objective-c ios

我尝试使用AFNetworking3.0上传图像.我试图找到如何从所有stackoverflow和教程上传图像,但所有这些都与AFNetworking2.0及更低版本相关.主要焦点是我要将图像上传到网站.

上传图片后,图片网址应该存在于网站上.

请参阅下面的代码.

AFHTTPRequestOperationManager *requestManager;

[requestManager POST:KAPIUploadOutfit parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) 
{
        [formData appendPartWithFileURL:fileURL name:@"photo_url" fileName:filename mimeType:@"image/png" error:nil];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        successCB(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        errorCB(CONNECTION_ERROR);
    }];
Run Code Online (Sandbox Code Playgroud)

我不确定参数是什么.请告诉我怎么做.

iVa*_*run 10

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

NSURLSessionUploadTask *uploadTask;
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
                  [progressView setProgress:uploadProgress.fractionCompleted];
              });
          }
          completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
              if (error) {
                  NSLog(@"Error: %@", error);
              } else {
                  NSLog(@"%@ %@", response, responseObject);
              }
          }];

[uploadTask resume];
Run Code Online (Sandbox Code Playgroud)

在此代码中,http://example.com/upload是api,其中图像将上传,file://path/to/image.jpg是您的本地图像路径.