【发布时间】:2013-07-30 04:02:45
【问题描述】:
我是 Objective-c 初学者,我尝试使用 RestKit 向服务发布一些内容
这是我要发布到网络服务的 json。
{
"image":"images/cover.jpg"
"text":"this is a cover pic"
}
我将发送一张 nsdata 格式的图片 image 将保存一个路径,我可以得到一个显示图片的路径。
点赞:ipAddress/images/cover.jpg
我定义了两个对象:TSImage 对象:
@interface TSImage : NSObject
@property(nonatomic, strong) NSData *image;
@property(nonatomic, copy) NSString *text;
@end
这是我创建帖子数据的代码
TSImage *testing = [[TSImage alloc] init];
NSURL *imageUrl = [NSURL URLWithString:@"https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-ash3/995399_571403339577816_2064708560_n.jpg"];
UIImage *urlImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:imageUrl]];
NSData *imageData = UIImageJPEGRepresentation(urlImage, 0.7);
testing.image = imageData;
testing.text = @"testingup";
NSURL *baseURL = [NSURL URLWithString:@""]; //for security
NSString* path = @"/image/";
RKObjectMapping *objectMapping = [RKObjectMapping requestMapping];
[objectMapping addAttributeMappingsFromArray:@[@"image",@"text"]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:objectMapping
objectClass:[TSImage class]
rootKeyPath:nil];
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:baseURL];
[manager addRequestDescriptor:requestDescriptor];
然后我尝试两种方式发布数据
第一种方法:
[manager postObject:testing path:path parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
NSLog(@"Loading mapping result: %@", result);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Operation failed with error: %@", error);
}];
我只向服务器发布文本
{
"image":""
"text" "testingup"
}
第二种方式:
NSMutableURLRequest *request =
[manager multipartFormRequestWithObject:testing method:RKRequestMethodPOST
path:path parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
[formData appendPartWithFileData:UIImageJPEGRepresentation(urlImage, 0.7)
name:@"expense[photo_file]"
fileName:@"photo.jpg"
mimeType:@"image/jpeg"];
}];
RKObjectRequestOperation *operation =
[manager objectRequestOperationWithRequest:request
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
// Success handler.
NSLog(@"Loading mapping result: %@",mappingResult);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
// Error handler.
RKLogError(@"Operation failed with error: %@", error);
}];
我不知道为什么它没有发布。
这是我的referenceRestKit
感谢您的收看。
【问题讨论】:
标签: iphone ios objective-c image restkit