ios RestKit 后附 UIImage
<p><p>我正在尝试使用 RestKit 将图像发布到我的服务器并映射来自服务器的响应。但是服务器总是支持我发送的那个文件是零。
谁能帮我解决这个问题?</p>
<pre><code>NSData *data = UIImageJPEGRepresentation(imageData, 1.0);
NSDictionary *params = [ initWithObjectsAndKeys:
data, @"avatar",
nil];
RKObjectMapping* objectMapping = ];
[objectMapping addAttributeMappingsFromDictionary:@{
@"message" : @"message"
}];
NSIndexSet *successStatusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objectMapping
method:RKRequestMethodPOST
pathPattern:nil
keyPath:nil
statusCodes:successStatusCodes];
RKObjectManager *objectManager = ];
[ setDefaultHeader:@"device_uid" value:];
forMIMEType:@"image/jpeg"];
;
;
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
;
[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);
[ setMessage:obj.message];
}
;
//[ postNotificationName:@"didRegistered" object:nil];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我已经解决了来自 RESTKit 本身的文档问题。
也许它会对其他人有所帮助:</p>
<pre><code> NSData *data = UIImageJPEGRepresentation(imageData, 0.3);
RKObjectMapping* objectMapping = ];
[objectMapping addAttributeMappingsFromDictionary:@{
@"message" : @"message"
}];
NSIndexSet *successStatusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objectMapping
method:RKRequestMethodPOST
pathPattern:nil
keyPath:nil
statusCodes:successStatusCodes];
RKObjectManager *objectManager = ];
[ setDefaultHeader:@"device_uid" value:];
;
NSMutableURLRequest *request = [ 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) {
];
;
];
];
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Failure");
}];
[ enqueueObjectRequestOperation:operation];
</code></pre></p>
<p style="font-size: 20px;">关于ios RestKit 后附 UIImage,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/29452000/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/29452000/
</a>
</p>
页:
[1]