Objective C自定义延迟加载图像UITableView单元格 [英] Objective C Custom Lazy Load Images UITableView Cell

查看:123
本文介绍了Objective C自定义延迟加载图像UITableView单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首次将远程图像加载到iPhone应用程序中,并希望有一些帮助优化该过程。我目前所做的是获取图像(如果它不存在)并缓存它。主要目标是:

First time loading remote images into an iPhone app, and would like some help optimizing the process. What I've currently done is get the image if it doesn't exist, and cache it. The major goals are to:


  • 仅在需要时加载图片。

  • 保存图片以备将来使用减少数据消耗,并允许用户在没有连接到互联网时拥有一个功能有点的应用程序。

我只是不这样做我认为我做得很好。

I just don't think I'm doing it well enough.

这是tableView中代码的片段:cellForRowAtIndexPath:

Here's a snippet of the code within tableView:cellForRowAtIndexPath:

MVImageCell * cell = (MVImageCell *)[tableView dequeueReusableCellWithIdentifier:@"PicsAndVideosCell"];

// empty cell
cell.imageView.image = nil;
cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;

// set cell properties
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 2;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.imageView.frame = CGRectMake(15, 6, 58, 58);
cell.imageView.layer.cornerRadius = 6;
cell.imageView.layer.masksToBounds = YES;

Photoset * sfc = [self.myarray objectAtIndex:indexPath.row];
cell.cid = sfc.sfcid;
cell.ctitle = sfc.title;
cell.cimg = sfc.cover;

cell.textLabel.text = sfc.title;
cell.detailTextLabel.text = sfc.date;

// set cell image
MVImage * thumb = [[MVImage alloc] init];
NSString * retina = ([[MVProject sharedInstance] settings_retina]) ? @"2" : @"";
if ([NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"Settings_SFCCovers%@_%@", retina, cell.cid]]]) {
    thumb = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"Settings_SFCCovers%@_%@", retina, cell.cid]]];

    [cell.imageView setImage:[MVImage imageWithImage:[[UIImage alloc] initWithData:thumb.data] covertToWidth:58.0f covertToHeight:58.0f]];
    [cell bringSubviewToFront:[cell.imageView superview]];
} else {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{
        dispatch_sync(dispatch_get_main_queue(), ^{
            thumb.data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", cell.cimg]]];
            thumb.title = cell.ctitle;
            [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:thumb] forKey:[NSString stringWithFormat:@"Settings_SFCCovers%@_%@", retina, cell.cid]];

            [cell.imageView setImage:[MVImage imageWithImage:[[UIImage alloc] initWithData:thumb.data] covertToWidth:58.0f covertToHeight:58.0f]];
            [cell bringSubviewToFront:[cell.imageView superview]];
        });
    });
}

return cell;

我应该使用SQLite数据库而不是NSUserDefaults吗?

Should I use a SQLite database instead of NSUserDefaults?

我也遇到了异步加载问题。我觉得它应该是这样的:

I'm also having trouble with the asynchronous loading. I feel like it's supposed to look like this:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
    thumb.data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", cell.cimg]]];
    thumb.title = cell.ctitle;
    [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:thumb] forKey:[NSString stringWithFormat:@"Settings_SFCCovers%@_%@", retina, cell.cid]];

    dispatch_sync(dispatch_get_main_queue(), ^{
        [cell.imageView setImage:[MVImage imageWithImage:[[UIImage alloc] initWithData:thumb.data] covertToWidth:58.0f covertToHeight:58.0f]];
        [cell bringSubviewToFront:[cell.imageView superview]];
    });
});

但这显然会将错误的图像数据保存到NSUserDefault目的地。

But that obviously saves the wrong image data to the NSUserDefault destination.

对此有任何帮助,我的编码风格指针以及其他任何内容都非常感谢。

Any help on this, pointers on my coding style, and anything else is greatly appreciated.

谢谢!

推荐答案

只需快速查看代码 - 您似乎将块推送到异步队列,但是您在这些块中调用UI代码。

Just having a quick look at your code - you seem to be pushing blocks onto asynchronous queues, but you are calling UI code in those blocks.

你应该只在主线程上运行UI代码。

You should only run UI code on the main thread.

至于解决方案 - 看看有些开放源代码实现要么让你知道你应该做什么,要么直接使用它们。

As for a solution - have a look at some of the open source implementations to either give you an idea of what you should be doing, or just use them directly.

其中一个是 AsyncImageView

还有其他人会提出快速搜索。

There are others that a quick search will bring up.

这篇关于Objective C自定义延迟加载图像UITableView单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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