通过JSON层次结构和objectForKey使用AFNetworking加载图像视图 [英] Loading imageviews with AFNetworking via JSON hierarchy and objectForKey

查看:70
本文介绍了通过JSON层次结构和objectForKey使用AFNetworking加载图像视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AFNetworking的新手,但是在许多SO问题之后...我得出的结论是,这是解决我的问题的方法.

Very new to AFNetworking, but after many SO questions... I've concluded that this is the solution to my issue.

我有一个JSON数据层次结构,它动态地加载了TableViewController单元.我所有的文本字符串都已正确加载,但我的URL中的图像未导入到我的图像视图中.

I've got a hierarchy of JSON data loading my TableViewController cells dynamically. All of my text strings are loading appropriately but the image from my URL is not making it to my imageview.

在使用AFNetworking库之前,我已经加载了图像,但是当我滚动并返回时(它们不得不再次加载.)我遇到了一些问题,即照片没有保持加载状态.

Before utilizing the AFNetworking library, I had the images loaded, but I was experiencing some issues with the photos not staying loaded when I scrolled away and returned (they had to load again.)

要在那里放图片我想念什么?

What am I missing to get my images in there?

TableViewController.m

-(void)viewDidLoad {
    [super viewDidLoad];

    // Set the side bar button action. When it's tapped, it'll show up the sidebar.
    siderbarButton.target = self.revealViewController;
    siderbarButton.action = @selector(revealToggle:);

    // Set the gesture
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

    self.tableView.backgroundColor = [UIColor whiteColor];
    self.parentViewController.view.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1];

    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo_app_white.png"]];

    NSURL *myURL = [[NSURL alloc]initWithString:@"http://domain.com/json2.php"];
    NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
    NSError *error;

    jsonArray = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:&error];

    [tableView reloadData]; // if tableView is unidentified make the tableView IBOutlet
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return jsonArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NeedCardTableViewCell *cell = (NeedCardTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"needCard"];

    if (cell == nil)
    {
        cell = [[NeedCardTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"needCard"];
    }

    NSDictionary *needs = jsonArray[indexPath.row]; // get the data dict for the row
    cell.textNeedTitle.text = [needs objectForKey: @"needTitle"];
    cell.textNeedPoster.text = [needs objectForKey: @"needPoster"];
    cell.textNeedDescrip.text = [needs objectForKey: @"needDescrip"];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"userImage" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        _imageProfPic.image = responseObject;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

    return cell;
}

推荐答案

通过使用本教程,设法解决这一问题: 通过AFNetworking轻松实现联网

Manage to figure this one out with the use of this tutorial: Networking Made Easy with AFNetworking

我使用了最后的代码片段来获得所需的结果:

I used the final snippet of code to get my desired result:

NSURL *url = [[NSURL alloc] initWithString:[movie objectForKey:@"artworkUrl100"]];
[cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder"]];

我剪切了他的代码的第二行,因为我的PHP/JSON

I cut the second line of his code because I already have an if statement in my PHP/JSON

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NeedCardTableViewCell *cell = (NeedCardTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"needCard"];

    if (cell == nil)
    {
        cell = [[NeedCardTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"needCard"];
    }

    NSDictionary *needs = jsonArray[indexPath.row]; // get the data dict for the row
    cell.textNeedTitle.text = [needs objectForKey: @"needTitle"];
    cell.textNeedPoster.text = [needs objectForKey: @"needPoster"];
    cell.textNeedDescrip.text = [needs objectForKey: @"needDescrip"];

    NSURL *url = [[NSURL alloc] initWithString:[needs objectForKey:@"userImage"]];
    [cell.imageProfPic setImageWithURL:url];

    return cell;
}

这就像一个魅力,并且由于我是AFNetworking的新秀,所以本教程非常有帮助.

It worked like a charm, and the tutorial was pretty helpful since I'm a rookie with AFNetworking.

这篇关于通过JSON层次结构和objectForKey使用AFNetworking加载图像视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆