0

1 - 如何使用带有 RestKit 客户端的 NSMutableDictionary 包含图片?

我的服务器在下面的代码中从 NSMutableDictionary 接收数据。

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:usernameTextfield.text forKey:@"username"];

[client put:@"/main/insert" params:params delegate:self];

但是如何同时附加图像?

另一方面,RKParams 在我的服务器端对我不起作用。

RKParams *params=[[RKParams alloc]init];
imageData = UIImagePNGRepresentation(imageField.image);
[params setData:imageData MIMEType:@"image/png" forParam:@"image"];

休息服务器代码:

public function insert_put()
{
    $username = $this->put('username');

    $this->model->insertPost($username);

    //Question number 2.        

    $data['success'] = 'added successfully';
    $this->response($data, 200);
}

2 - 我如何从我的服务器(php)接收它?我正在使用 put 选项。

4

2 回答 2

0

我只是做以下

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

}
于 2012-06-26T13:23:49.800 回答
0

更好的问题。谢谢!我删除了我的反对票。

您的 RestKit 代码看起来不错,我认为是您的 php 代码失败了。RestKit 会将图像作为 multipart/form-data 的一部分上传。此信息不会像您的用户名那样出现在您的正常 _PUT 数组中。我不确定您使用的是什么框架(那是 REST 控制器库吗?),但您可以使用 _FILES['image'] 或您的服务器框架使用的任何东西访问该文件。$this->files('image') 也许?

编辑:

要创建混合参数文件,请尝试以下操作:

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

但是现在你不能使用$this->put,而是使用$this->upload。例如:

$data = $this->upload->data();
$username = $data['username'];
$imagefile = $data['image'];
于 2012-06-25T13:41:52.293 回答