0

嗨,我是编程新手。我的问题是我无法解析图像。需要将图像解析为带有文本的列表。

我正在使用 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;
}
4

2 回答 2

1

从 Json 获取图像的绝对服务器路径,例如:

https://www.google.co.in/images/srpr/logo3w.png

或者

定义基本服务器路径,如:

#define baseImgurl  @"https://www.google.co.in/images/srpr/"

加入

NSString *imgurl = [NSString stringWithFormat:@"%@%@",baseImgurl,[imagesArray objectAtIndex:indexPath.row]];

cell.imageView.image = [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgurl]]]

为避免延迟加载,请使用此

#import <SDWebImage/UIImageView+WebCache.h>

...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:MyIdentifier] autorelease];
    }

     NSString *imgurl = [NSString stringWithFormat:@"%@%@",baseImgurl,[imagesArray objectAtIndex:indexPath.row]];

    [cell.imageView setImageWithURL:[NSURL URLWithString:imgurl]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    return cell;
}
于 2013-01-08T08:49:59.887 回答
0

你需要改变这一行

cell.imageView.image = = [imagesArray objectAtIndex:indexPath.row];

作为

cell.imageView.image =  [UIImage imageNamed:[imagesArray objectAtIndex:indexPath.row]];

您需要在应用程序包中包含图像的另一件事,否则需要在应用程序缓存文件夹中下载图像,然后创建图像路径,然后尝试加载图像

于 2013-01-08T06:56:40.333 回答