簡體   English   中英

通過JSON層次結構和objectForKey使用AFNetworking加載圖像視圖

[英]Loading imageviews with AFNetworking via JSON hierarchy and objectForKey

AFNetworking非常新的AFNetworking ,但是在遇到許多SO問題之后...我得出結論,這是解決我的問題的方法。

我有一個JSON數據層次結構,可動態加載TableViewController單元。 我所有的文本字符串均已正確加載,但URL的圖像未導入到我的imageview中。

在使用AFNetworking庫之前,我已經加載了圖像,但是當我滾動並返回時(它們不得不再次加載)時,我遇到了一些問題,即照片沒有保持加載狀態。

我缺少將圖像放在那里的什么功能?

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 + UIImageView類別? 然后,您可以致電:

[_imageProfPic setImage:[NSURL URLWithString:@"http://myimageurl.com/imagename.jpg"]];

這將發出請求,然后將返回的圖像設置為UIImageView的UIImage,而無需執行其他任何操作。 您還應該考慮在AppDelegate中初始化NSURLCache:

NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
                                                  diskCapacity:10 * 1024 * 1024
                                                      diskPath:nil];
[NSURLCache setSharedURLCache:cache];

看一下NSHipsterNSURLCache上的運行情況 這將有助於第二次更快地重新加載圖像和所有請求。 在處理圖像和表格時,這一點變得越來越重要。

通過使用本教程,設法弄清楚這一點: 使用AFNetworking輕松實現聯網

我使用了最后的代碼片段來獲得所需的結果:

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

我剪掉了他的代碼的第二行,因為我的PHP / JSON已經有一個if語句

- (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的新手, AFNetworking該教程非常有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM