Error while uploading images taken from camera to server AFNetworking 3.0

73 Views Asked by At

I am using AFNetworking to send images to my server. When I pick images from Gallery every thing works fine. But when I pick image using Camera, the server sends me an error that its an invalid image.

  • Using UIImagePickerController to pick images from camera.

Code

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

Did Finish Launching with options Code

[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

There are 1 best solutions below

2
On

Use below method when uploading through camera.

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

}