2013-01-08 49 views
0

嗨,我是編程新手。 我的問題是,我無法解析圖像。圖像需要用文本解析成列表。在ios中Json圖像解析錯誤。

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

要分析的JSON是無效的,它缺少一個屬性。 – Musa

+0

嘿,你需要喲帕斯一些網址來獲取imagem而不是圖像名稱。 – Rajneesh071

回答

0

你需要改變這一行

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

作爲

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

你需要有像在您的應用程序包,否則需要下載圖像的應用程序的緩存文件夾,然後進行路徑高達圖像,然後嘗試加載圖片一件事

+0

我很抱歉,但我真的很新。如果有,我是否還需要添加? –

+0

NSData * img = [[NSData alloc] initWithContentsOfURL:url]; UIImage * image = [UIImage imageWithData:img]; –

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]]] 

加入爲了避免延遲加載使用這種library

#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; 
}