在UITableView的几个图像的延迟加载(Lazy Loading of several ima

2019-06-26 10:28发布

什么是下载一些图片,并加载他们每个人在一个UIImageView这是一个UITableViewCell内的最佳实践? 特别是,我应该调整/下载后更换的UIImageView或者我应该调整图像大小以适合的UIImageView。 需要注意的是尺寸/更换的UIImageView也调整/替换的UITableViewCell。 它会导致任何问题?

Answer 1:

一对夫妇的想法:

什么是下载一些图片,并加载它们中的每一个最佳实践UIImageView这是一个内部UITableViewCell

关于图像的下载的最佳做法包括:

  1. 当然利用自己懒加载(加载图片,你需要他们,而不是之前)。

  2. 异步下载图像。

  3. 请确保您的下载技术将取消不再可见的tableview细胞的请求。 例如,如果你是一个缓慢的网络上,并迅速回落在你的表视图滚动,你不想占用你的设备下载的是不再可见的图像。

  4. 确保你不要让你的服务器的太多的并发请求。 在iOS中,当你超过5个或6的并发请求,后续请求将冻结,直到那些之前完成。 在最坏的情况下,后续请求将真正开始失败,因为他们超时。

  5. 缓存结果。 最起码,它们缓存在内存中。 您可能还需要将它们缓存到永久存储(又名“盘”),太。

如果你打算写的异步操作自己的代码,缓存等你可能想使用NSOperationQueue而不是GCD这样我就可以限制的背景请求数 ,并请求取消 。 你可以使用NSCache缓存图像。 你可能会使用一个UITableViewCell子类(或类别),这样就可以节省weak参考“上一个”操作,让你可以取消任何不完整的,之前的请求。

正如你所看到的,这是不平凡的,我想你使用现有的建议UIImageView类,如可作为的一部分SDWebImageAFNetworking 。 恕我直言,前者是一个小更丰富(如提供磁盘缓存),但如果你做了很多的网络,并希望坚持使用一个单一的框架, AFNetworking做了伟大的工作,太。

后来你问:

特别是,我应该调整/下载后更换的UIImageView或者我应该调整图像大小以适合的UIImageView。 需要注意的是尺寸/更换的UIImageView也调整/替换的UITableViewCell。 它会导致任何问题?

  1. 如果你的图像比你的细胞的缩略图视图需要更大的,你有两种方法。 首先,你可以使用contentModeUIViewContentModeScaleAspectFitUIViewContentModeScaleAspectFill (如果你使用AspectFill ,请确保您还设置clipsToBoundsYES )。 更妙的是,你实际上可以调整图像大小 ,你下载后。

  2. 这是个人意见,但我认为这是一个更好的UX有一个UIImageView对细胞固定大小的,然后当异步镜像下载完成后,只需设置image的财产UIImageView 。 你需要的图片,以优雅出现在你的用户界面,因为它们可以下载,但你通常不希望视图的一个不和谐的重新布局,而用户已经在阅读的过程中那里的东西。 如果你的设计绝对必要的细胞的重新布局,那么你可以调用reloadRowsAtIndexPaths



Answer 2:

在一个懒加载图像常见的做法UITableViewCell是使用一个通知回调,让UITableViewCell时候已经接收到的图像知道。

从本质上讲,你要创建的子类UIImageView有一个IMAGEURL字段,改变的情况下,触发关闭的图像的请求,并用它来代替标准UIImageView

接口的子类的UIImageView:

@property (nonatomic, copy) NSString *imageURL;

实现了UIImageView的子类:

//synthesize property
@synthesize imageURL = _imageURL;

- (void)setImageURL:(NSString *)imageURL {
    if(_imageURL)
        [[NSNotificationCenter defaultCenter] removeObserver:self name:_imageURL object:nil];
    _imageURL = [imageURL copy];
    //if imageURL is valid...
    if(_imageURL.length) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveImage:) name:_imageURL object:nil];
        //fire off some asynchronous image fetch
        //when the image fetch completes, sent off a notification using the imageURL as the notification name
        //It's up to you to create the implementation for this yourself
        ... [MyImageManager fetchImage:_imageURL notificationName:_imageURL]; 
    }
}

- (void)didReceiveImage:(NSNotification*)notification
{
    //handle your received image here
    if([notification.object isKindOfClass:[UIImage class]])
    {
        self.myCustomImageView.image = notification.object;
    }
}

然后在UITableViewCell类,当你重写prepareForReuse:

- (void)prepareForReuse {
    [super prepareForReuse];
    self.myCustomImageView.imageURL = nil;
    self.myCustomImageView.image = nil;
    //do the rest of your prepareForReuse here:
    ...
}

现在,就为图像的整体大小调整VS的ImageView的大小时,什么你应该做的是独自离开的ImageView大小,并使用contentMode属性来处理不同尺寸所产生的图像。 你可能值是:

UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,
UIViewContentModeScaleAspectFill,
UIViewContentModeRedraw,
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,

每个人都有自己各自的结果-你可能会想使用UIViewContentModeScaleAspectFit因为这将重新大小的图像以适合ImageView的,不扭曲它-这可能会留下空利润率的ImageView的大小或顶部/底部。 看点填充会做类似的事情,只是它会调整图片的大小要大到足以填满整个ImageView的,并可能切断面或图像的顶部/底部。 规模,以填补只会拉伸图像,以填补ImageView的。



文章来源: Lazy Loading of several images in UITableView