AFNetworking Json post image

问题描述:

我学习了客观的C编程。我不知道如何将图像从库中发布到api json(我使用UIImagePickerController从库中获取图片)。谢谢!AFNetworking Json post image

我的JSON API: http://i.stack.imgur.com/DLKZG.png

- (IBAction)btnAddBook:(id)sender { 


AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://192.168.1.54"]]; 

NSData *imageData = UIImageJPEGRepresentation(self.ivPickedImage.image, 1); 
NSMutableDictionary *params = [[NSMutableDictionary alloc]init]; 

[params setValue:self.tfTitle.text forKey:@"title"]; 
[params setValue:self.tfPrice.text forKey:@"price"]; 
[params setValue:self.tfProem.text forKey:@"proem"]; 
[params setValue:@"Vietnamese" forKey:@"language"]; 

AFHTTPRequestOperation *op = [manager POST:@"/api/books" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 

    [formData appendPartWithFileData:imageData name:@"file" fileName:@"photo.jpg" mimeType:@"image/jpeg"]; 
} success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject); 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@ ***** %@", operation.responseString, error); 
}]; 
[op start]; 

请使用如下代码:

NSDictionary *requestDictionary = [NSDictionary dictionaryWithObjectsAndKeys:abc.text, @"firstkey",xyz.text, @"secondkey" nil]; 

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"YOUR API URL" parameters:requestDictionary constructingBodyWithBlock:^(id<AFMultipartFormData> formData) 
    {                 [formData appendPartWithFileData:imageData name:@"profile_pic" fileName:@"photo.jpg" mimeType:@"image/jpeg"]; 
    } 
    } error:nil]; 

    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 
    NSProgress *progress = nil; 

    NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { 
      NSLog(@"%@", response); 
      if (error) { 
      } else { 
      } 
}]; 
[uploadTask resume]; 

希望这有助于。

通过使用AFNetworking多部分请求。您可以将图像作为数据发布。下面是使用AFNetworking

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

AFNetworking

这里如何上传图片作为多完成的块图像不为JSON通过。通过使用AFNetworking多部分请求,您的所有请求参数都以JSONImage/Video的形式传递,并以NSData的形式传递。 NSData被分成多个数据包。

谢谢