问题
1 - How can I include a picture using NSMutableDictionary with RestKit Client?
My server receives data from NSMutableDictionary in my code below.
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:usernameTextfield.text forKey:@"username"];
[client put:@"/main/insert" params:params delegate:self];
but how can I attach an image at the same time?
On the other hand, RKParams doesn't work for me on my server side.
RKParams *params=[[RKParams alloc]init];
imageData = UIImagePNGRepresentation(imageField.image);
[params setData:imageData MIMEType:@"image/png" forParam:@"image"];
Rest server code:
public function insert_put()
{
$username = $this->put('username');
$this->model->insertPost($username);
//Question number 2.
$data['success'] = 'added successfully';
$this->response($data, 200);
}
2 - How can I receive it from my server(php) ? I'm using put option to this.
回答1:
Much better question. Thank you! I removed my down vote.
Your RestKit code looks fine, I think it is your php code that is failing. RestKit will upload the image as part of a multipart/form-data. This info will not appear in your normal _PUT array like your username did. I'm not sure what frameworks you are using (Is that the REST Controller library?), but you can access the file using _FILES['image'] or whatever your server framework uses. $this->files('image') maybe?
EDIT:
To create a mixed param file try this:
NSDictionary* strings = [NSDictionary dictionaryWithObjectsAndKeys: usernameTextfield.text, @"username", nil];
NSData* image = UIImagePNGRepresentation(imageView.image);
RKParams* params = [[RKParams alloc] initWithDictionary: strings];
[params setData: image MIMEType: @"image/png" withParameter: @"image"];
//And send it
[client put:@"/main/insert" params:params delegate:self];
But now you can not use $this->put, but instead $this->upload. For example:
$data = $this->upload->data();
$username = $data['username'];
$imagefile = $data['image'];
回答2:
I just do the following
- (void) upload: (UIImage *) pic onLoad:(RKObjectLoaderDidLoadObjectBlock) loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock{
RKParams* imageParams = [RKParams params];
NSData* imageData = UIImageJPEGRepresentation(pic, 0.7f);
[imageParams setData:imageData MIMEType:@"image/jpg" forParam:@"FileUpload"];
NSString *resourcePath = @"/api/upload/";
RKObjectMapping *mapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[KFMedia class]];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:resourcePath usingBlock:^(RKObjectLoader *loader) {
loader.method = RKRequestMethodPOST;
loader.params = imageParams;
[self settingsForLoader:loader withMapping:mapping onLoad:loadBlock onError:failBlock];
}];
}
- (void) settingsForLoader: (RKObjectLoader *) loader withMapping: (RKObjectMapping *) mapping onLoad:(RKObjectLoaderDidLoadObjectBlock) loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock{
loader.objectMapping = mapping;
loader.delegate = self;
loader.onDidLoadObject = loadBlock;
loader.onDidFailWithError = ^(NSError * error){
//NSLog(@"%@",error);
};
loader.onDidFailLoadWithError = failBlock;
loader.onDidLoadResponse = ^(RKResponse *response) {
[self fireErrorBlock:failBlock onErrorInResponse:response];
};
}
来源:https://stackoverflow.com/questions/11189120/include-image-using-nsmutabaledictionary-with-rkclient-and-receiving-on-the-php