嗨,我是编程新手。我的问题是我无法解析图像。需要将图像解析为带有文本的列表。
我正在使用 JSONKit 来解析 json。我有旧版本的 xcode,只有模拟器 4.3。
我可以解析文本但不能解析图像。我查看了一些关于如何解析图像的示例,但未能正确执行。
我要解析的json:
{[{"n_id":"1","n_name":"Green","n_text":"this is test","Image":"dasdas.jpg"}]}
我想我需要使用这样的东西
NSData *img=[[NSData alloc]initWithContentsOfURL:url];
UIImage *image=[UIImage imageWithData:img];
但我不知道如何将它合并到我的代码中。我有点失落。
提前致谢
////Json2ViewController.h
@interface Json2ViewController : UIViewController {
NSArray * list;
NSString * content;
NSString * many;
NSString * thumbNail;
NSMutableArray *studName;
NSMutableArray *ntext;
NSMutableArray *imagesArray;
NSArray *images;
}
@property (nonatomic, retain) NSMutableArray* studName;
@property (nonatomic, retain) NSMutableArray* ntext;
@property (nonatomic, retain) NSMutableArray* imagesArray;
//Json2ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
NSData * JSONdata =[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://myurl.com"]];
if([JSONdata length] == 0){
[JSONdata release];
return;
}
NSArray *students =[JSONdata objectFromJSONData];
studName = [[NSMutableArray alloc] init];
ntext = [[NSMutableArray alloc] init];
imagesArray = [[NSMutableArray alloc] init];
for(NSDictionary *student in students)
{
content = [student objectForKey:@"n_name"];
if(content)
[studName addObject:content];
many = [student objectForKey:@"n_text"];
if(many)
[ntext addObject:many];
images = [student objectForKey:@"Image"];
if(images)
[imagesArray addObject:images];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.imageView.image = = [imagesArray objectAtIndex:indexPath.row];
cell.textLabel.text = [studName objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [ntext objectAtIndex:indexPath.row];
return cell;
}