我正在使用AFNetworking将图像发送到我的服务器。当我从Gallery中选择图像时,每件事情都可以。但是当我使用Camera选择图像时,服务器会向我发送一个错误,指出它是无效的图像。
代码
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;
答案 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];
}