将字典数组上传到服务器然后下载

时间:2016-07-26 08:17:39

标签: ios download upload nsmutablearray

我目前正在制作一个视频编辑部分,用于捕捉字典中的屏幕截图和一些关键值。录制完成后,我会收到一系列词典。

现在我希望这个字典数组上传到服务器,然后我想下载该数组。

我尝试将NSMutableArray转换为字节并将其写入文本文件,然后下载文本文件并将其转换回NSMutableArray

问题是NSMutableArray内的词典没有键值。

在服务器上上传和下载此信息的其他更好方法是什么?

1 个答案:

答案 0 :(得分:0)

您应该分开您的信息和图片。 然后上传它们。

有一个例子:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

//------------------This part is for upload------------------
//Upload info
UIImage* sourceImage = [UIImage imageNamed:@"source.png"];
UIImageView* imgV1 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
imgV1.backgroundColor = [UIColor redColor];
imgV1.image = sourceImage;
[self.view addSubview:imgV1];

NSDictionary* uploadDic = [[NSDictionary alloc] initWithObjectsAndKeys:
                           @"source.png",@"testImage",
                           nil];
NSData* uploadData = [NSJSONSerialization dataWithJSONObject:uploadDic options:NSJSONWritingPrettyPrinted error:nil];
NSString* uploadString = [[NSString alloc] initWithData:uploadData  encoding:NSUTF8StringEncoding];

//Upload image
NSData* imageData = [NSData dataWithContentsOfFile:@"source.png"];
[self uploadInfoToURL:uploadString andImageData:imageData];
//-----------------------------------------------------------

//------------------This part is for upload------------------
//Download info
NSString *downLoadString = uploadString;
NSData* downLoadData = [downLoadString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *downLoadDic = [NSJSONSerialization JSONObjectWithData:downLoadData options:NSJSONReadingMutableContainers error:nil];
UIImageView* imgV2 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 250, 100, 100)];
imgV2.backgroundColor = [UIColor blueColor];
[self.view addSubview:imgV2];

//Download image
NSString* downURL = [NSString stringWithFormat:@"http://youServer/%@",[downLoadDic objectForKey:@"testImage"]];
imgV2.image = [self getImageFromURL:downURL];
//-----------------------------------------------------------
}

-(UIImage *) getImageFromURL:(NSString *)fileURL {
NSLog(@"Begin download");
UIImage * result;
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];
return result;
}

-(BOOL *) uploadInfoToURL:(NSString *)infoString andImageData:(NSData *)data {
NSLog(@"Begin upload");
//Use http post to upload your image data to you server
//    if (nil == error) {
//        return YES;
//    }
return NO;
}

希望它可以帮到你。