2015-03-25 53 views
1

我無法在json中獲得我的照片,我是否沒有錯誤得到它?我已經檢查我的照片網址在PHP中,它的工作原理,json照片路徑xcode

在我tableview.m文件:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    DetailHomeViewController * dvc = [[DetailHomeViewController alloc]init]; 

    HomeDetail * currentHome = [HomeArray objectAtIndex:indexPath.row]; 

    dvc.photodetail = currentHome.photo_path; 

    NSURL *url = [NSURL URLWithString:dvc.photodetail]; 
    NSData *data = [NSData dataWithContentsOfURL:url]; 

    dvc.photolabel.image = [UIImage imageWithData:data]; 

    [self presentViewController:dvc animated:YES completion:nil]; 
} 


#pragma mark - Methods 
- (void) retrieveData 
{ 
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 
    NSURL * url = [NSURL URLWithString:getDataURL]; 
    NSData * data = [NSData dataWithContentsOfURL:url]; 
    json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 
    HomeArray = [[NSMutableArray alloc]init]; 

    for (int i = 0; i < json.count; i++) 
    { 

     NSString * photo = [[json objectAtIndex:i] objectForKey:@"photo_path"]; 


     HomeDetail * home2 = [[HomeDetail alloc]initWithPhoto_path:photo]; 

     [HomeArray addObject:home2]; 
    } 

    [self.myTableView reloadData]; 
} 

我HomeDetail.h文件:(JSON文件)

@property (nonatomic, strong) NSString * photo_path; 

//Methods 
- (id) initWithPhoto_path: (NSString *) photo ; 

我HomeDetail。 M檔:

- (id) initWithPhoto_path: (NSString *) photo ; 
{ 
    self = [super init]; 
    if (self) 
    {photo_path = photo;} 
    return self; 
} 

我DetailHomeViewController.h文件:

@property (nonatomic, strong) NSString * photodetail; 
@property (strong, nonatomic) IBOutlet UIImageView *photolabel; 

我DetailHomeViewController.m文件:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; 

    // photolabel.image = photodetail; 


    NSURL *url = [NSURL URLWithString:photodetail]; 
    NSData *data = [NSData dataWithContentsOfURL:url]; 

    photolabel.image = [UIImage imageWithData:data]; 
} 

,這裏是我的JSON數據:

[{"id":"2","detail_english":" TESTING_DETAIL","photo_path":"img\/uploads\/news\/news_20150320075842.png","date":"2013-10-15"‌​,"display_stat":"1","news_order":"1"}, 
{"id":"5","detail_english":"empty","photo_path":"img\/uploads\/news\/news_20150323105547.png","date":"2015-03-20","display_stat":"1","news_order":"1"}] 
+0

我能有一個正確的答案,因爲我讀的蘋果開發文檔了,但我仍然不知道自己做錯了什麼我〜 – 2015-03-27 11:10:08

+0

這是什麼都做與'Xcode IDE'? – Popeye 2015-03-27 11:40:22

+0

JSON數據在哪裏? – Popeye 2015-03-27 11:40:50

回答

1

你的照片路徑是不完整的URL,你需要添加域名。

所以你的字符串應該是這樣的:

http://www.domain.com/img/uploads/news/news_20150323105547.png 
+0

這張照片在我的數據庫文件中,所以我的json文件不應該有域名,它不正確? – 2015-03-27 14:56:21

+0

@MatchTsui如果你不把域名如何讓你的應用程序知道如何在服務器上找到它 – meda 2015-03-27 14:58:00

+0

我嘗試追加域名在我的網址,它的工作原理,這意味着我需要改變我的照片鏈接在JSON文件? – 2015-03-27 15:09:50