• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios RestKit 后附 UIImage

[复制链接]
菜鸟教程小白 发表于 2022-12-13 10:29:16 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试使用 RestKit 将图像发布到我的服务器并映射来自服务器的响应。但是服务器总是支持我发送的那个文件是零。 谁能帮我解决这个问题?

NSData *data = UIImageJPEGRepresentation(imageData, 1.0);



NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:
                        data, @"avatar",
                        nil];


RKObjectMapping* objectMapping = [RKObjectMapping mappingForClass:[UpdateAvatar class]];
[objectMapping addAttributeMappingsFromDictionary{
                                                    @"message" : @"message"
                                                    }];

NSIndexSet *successStatusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMappingbjectMapping
                                                                                        method:RKRequestMethodPOST
                                                                                   pathPattern:nil
                                                                                       keyPath:nil
                                                                                   statusCodes:successStatusCodes];

RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[self defineBaseUrl]];
[[objectManager HTTPClient] setDefaultHeader"device_uid" value:[self getDeviceUid]];
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType"image/jpeg"];
[objectManager setAcceptHeaderWithMIMEType"multipart/form-data"];
[objectManager setRequestSerializationMIMEType:RKMIMETypeJSON];
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
[objectManager addResponseDescriptor:responseDescriptor];
[objectManager postObject:nil path"/api/upload-avatar" parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"%@", mappingResult.array);

    for (UpdateAvatar *obj in mappingResult.array) {

        NSLog(@"MESSAGE: %@", obj.message);

        [[DataManager sharedManager] setMessagebj.message];

    }

    [SVProgressHUD showSuccessWithStatus"rofile Picture Updated"];
    //[[NSNotificationCenter defaultCenter] postNotificationName"didRegistered" object:nil];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"%@", error);
}];



Best Answer-推荐答案


我已经解决了来自 RESTKit 本身的文档问题。 也许它会对其他人有所帮助:

    NSData *data = UIImageJPEGRepresentation(imageData, 0.3);

RKObjectMapping* objectMapping = [RKObjectMapping mappingForClass:[UpdateAvatar class]];
[objectMapping addAttributeMappingsFromDictionary{
                                                    @"message" : @"message"
                                                    }];

NSIndexSet *successStatusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMappingbjectMapping
                                                                                        method:RKRequestMethodPOST
                                                                                   pathPattern:nil
                                                                                       keyPath:nil
                                                                                   statusCodes:successStatusCodes];

RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[DataManager defineBaseUrl]];
[[objectManager HTTPClient] setDefaultHeader"device_uid" value:[self getDeviceUid]];
[objectManager addResponseDescriptor:responseDescriptor];

NSMutableURLRequest *request = [[RKObjectManager sharedManager] multipartFormRequestWithObject:nil method:RKRequestMethodPOST path"/api/upload-avatar" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:data
                                name:@"avatar"
                            fileName:@"image.jpeg"
                            mimeType:@"image/jpeg"];
}];

RKObjectRequestOperation *operation = [objectManager objectRequestOperationWithRequest:request success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

    for (UpdateAvatar *obj in mappingResult.array) {

        [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0.31 green:0.31 blue:0.31 alpha:0.5]];
        [SVProgressHUD setRingThickness:2];
        [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
        [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"Uploading avatar\n %@", obj.message]];
    }


} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Failure");
}];

[[RKObjectManager sharedManager] enqueueObjectRequestOperationperation];

关于ios RestKit 后附 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29452000/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap