我學習了客觀的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];