将从相机拍摄的图像上传到服务器AFNetworking 3.0时出错

时间:2016-12-26 11:48:46

标签: ios objective-c uiimagepickercontroller afnetworking-3

我正在使用AFNetworking将图像发送到我的服务器。当我从Gallery中选择图像时,每件事情都可以。但是当我使用Camera选择图像时,服务器会向我发送一个错误,指出它是无效的图像。

  • 使用UIImagePickerController从相机中挑选图像。

代码

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
[manager.requestSerializer setValue:API_KEY_VALUE forHTTPHeaderField:API_KEY];


NSMutableURLRequest * request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:URL parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData)
                                 {
                                     UIImage *image = nil;
                                     for (int i=0; i < [imagesArr count]; i++)
                                     {
                                         image = [imagesArr objectAtIndex:i];
                                         //            image = [UIImage imageNamed:[NSString stringWithFormat:@"%d", i+1]];
                                         NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
                                         NSString * paramName = [NSString stringWithFormat:@"scrappygram_image[%d]",i];

                                         [formData appendPartWithFileData:imageData name:paramName
                                                                 fileName:[NSString stringWithFormat:@"image_%d",i]
                                                                 mimeType:@"image/jpeg"];
                                     }
                                 } error:nil];


[[manager dataTaskWithRequest:request completionHandler:^(NSURLResponse  response, id responseObject, NSError  error)
  {
      if (error)
      {
          NSLog(@"Error: %@", error.localizedDescription);
          [self.delegate apiFailedWithError:error andType:type];
      } else
      {
          NSLog(@"%@", responseObject);
          [self.delegate apiSuccessWithRespoonseDictionary:responseObject andType:type];
      }
  }] resume];

使用选项代码

完成启动
[GMSPlacesClient provideAPIKey:GMSPLACES_API_KEY];
[GMSServices provideAPIKey: GMSPLACES_API_KEY];
[[IQKeyboardManager sharedManager] setEnable:YES];
[[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:true];
[self settingsForRemoteNotifications];

[[NSUserDefaults standardUserDefaults] setValue:DEBUG_API_FAILED_ERROR_SHOW_YES forKey:DEBUG_ERROR_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];

return YES;

1 个答案:

答案 0 :(得分:-1)

通过相机上传时使用以下方法。

- (void)selectPhotoFromCamera
{

    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    //imagePickerController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeImage, nil];
    imagePickerController.delegate = self;
    [self presentViewController:imagePickerController animated:YES completion:nil];

}