【问题标题】:AFNetworking send image as file not as dataAFNetworking 将图像作为文件而不是数据发送
【发布时间】:2017-02-24 19:23:11
【问题描述】:

您好,我正在发送数据的服务器期望图像为文件(jpg)而不是 NSData。此代码有效,但服务器无法将 NSData 识别为图像。任何想法如何解决这个问题?

+ (void)signupWithParameters:(NSDictionary *)parameters
            andUserHaveImage:(BOOL)userHaveImage
                    andImage:(NSData *)image
                successBlock:(void (^) (NSDictionary *response))successHandler
                  errorBlock:(void (^) (NSDictionary *error))errorHandler
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"multipart/form­data" forHTTPHeaderField:@"Content-Type"];


    [manager POST:[NSString stringWithFormat:@"%@/api/1.0/customer/sign-up", DEFAULT_URL] parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        if (userHaveImage == YES) {
//  

      [formData appendPartWithFileData:image name:@"img_profile" fileName:@"profileImage.jpg" mimeType:@"image/jpg"];
        [formData appendPartWithFormData:image name:@"img_profile"];
    }

} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    successHandler(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    errorHandler(@{});
}];

}

【问题讨论】:

  • Stefan 您是通过 UIImageJPEGRepresentation 还是 UIImagePNGRepresentation 将图像转换为数据?
  • NSData *imageData = UIImageJPEGRepresentation(self.profileImageView.image, 1);是的,那个服务器人告诉我他看到了这些数据,但他期待的是文件(image.jpg)
  • meme 类型,试试这个@"image/jpeg"
  • 也试过了,同样的问题...谢谢建议...
  • [formData appendPartWithFormData:图像名称:@"img_profile"];这不应该存在

标签: ios objective-c uiimage afnetworking nsdata


【解决方案1】:

通过将图像转换为 UTF8 字符串并以该格式发送来解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 1970-01-01
    • 2011-05-07
    相关资源
    最近更新 更多