json照片路径xcode

时间:2015-03-25 02:37:11

标签: objective-c json

我无法在json中获取我的照片,我没有出错吗?我已经在php中检查了我的照片网址,它有效,

在我的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 ;

my 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"}]

1 个答案:

答案 0 :(得分:1)

您的照片路径不是完整网址,您需要附加域名。

所以你的字符串看起来像这样:

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