gpt4 book ai didi

ios RestKit 发布附加的 UIImage

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:41:04 25 4
gpt4 key购买 nike

我正在尝试使用 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 responseDescriptorWithMapping:objectMapping
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] setMessage:obj.message];

}

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

最佳答案

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

    NSData *data = UIImageJPEGRepresentation(imageData, 0.3);

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

NSIndexSet *successStatusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objectMapping
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] enqueueObjectRequestOperation:operation];

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com