【问题标题】:How to improve UITableView performance when have a lot of images?当有很多图像时如何提高 UITableView 的性能?
【发布时间】:2014-09-03 11:11:11
【问题描述】:

这是我的代码:

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

{
    static NSString *MyIdentifier = @"custom";

    SampleTableCell*cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
        cell = [[SampleTableCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:MyIdentifier];
    }

    User *user = [tableData objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];

    cell.name_Label.text = user.event_name;
    cell.description_Label.text = user.event_description;
    cell.venue_Label.text = user.event_venue;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
    dispatch_async(queue, ^{
                   [[cell imageView] setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:arr[indexPath.row]]]]];

        dispatch_sync(dispatch_get_main_queue(), ^{
            [[cell imageView] setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:arr[indexPath.row]]]]];
            [cell setNeedsLayout];
        });
    });
    return cell;
}

这是我获取数据的委托方法:

-(void)repaint:(NSMutableArray *)retrievedData
{
    if (retrievedData.count > 0)
    {
        userObj = [retrievedData objectAtIndex:0];

        for (userObj in retrievedData)
        {
            NSLog(@"%@is the value",retrievedData);

            url_Img1=@"http://kiascenehai.pk/assets/uploads/event-images/50x50-thumb/";
            url_Img2=userObj.event_dpURL;
            url_Img_FULL = [url_Img1 stringByAppendingPathComponent:url_Img2];
            [arr addObject:url_Img_FULL];
            NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
            [userDefault setObject:userObj.event_dpURL forKey:@"dpURL"];
            [userDefault synchronize];
        result = [userDefault objectForKey:@"dpURL"];
        NSLog(@"Show url_Img_FULL: %@",url_Img_FULL);
        [tableData addObjectsFromArray:retrievedData];
        [table reloadData];
    }

【问题讨论】:

  • 当我滚动表格时;它在加载新单元格时显示抽搐。我需要一些方法来改善它的滚动,就像 facebook 应用程序一样。
  • 你应该使用SDWebImage 库。它会让你的tableview非常流畅。我自己在很多应用程序中都使用过它..
  • Ahmed Z.但我无法使用它..我是 ios 开发的新手,它超越了我的想法..需要一些其他想法
  • 没有其他想法或方法可以让它更流畅......

标签: iphone uitableview ios7


【解决方案1】:

许多第三方源代码可用于 iOS 中的延迟图像加载:

查看以下网址:

https://github.com/rs/SDWebImage

它一定对你有帮助。

【讨论】:

  • 有没有其他更快的方法来喜欢使用情节提要或代码中的某些选项;因为我已经创建了所有项目,它会越过它的最后期限
  • 它非常简单。您可以在所有 UITableView、UICollectionView 和 UIImageView 中使用此延迟图像加载。
【解决方案2】:

感谢 Olivier Poitrey 我们可以使用sdwebimage
它将用于异步获取图像,您也可以缓存图像。希望对你有帮助

如何使用

API 文档可在 CocoaDocs - SDWebImage 中获得

在 UITableView 中使用 UIImageView+WebCache 类别

只需#import UIImageView+WebCache.h 头文件,然后调用 setImageWithURL:placeholderImage: 方法来自 tableView:cellForRowAtIndexPath: UITableViewDataSource 方法。 从异步下载到缓存,一切都会为您处理 管理。

#import <SDWebImage/UIImageView+WebCache.h>
> 
> ...
> 
> - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
>     static NSString *MyIdentifier = @"MyIdentifier";
> 
>     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
> 
>     if (cell == nil)
>     {
>         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
>                                        reuseIdentifier:MyIdentifier] autorelease];
>     }
> 
>     // Here we use the new provided setImageWithURL: method to load the web image
>     [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
>                    placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
> 
>     cell.textLabel.text = @"My Text";
>     return cell; }

使用块

使用块,您可以收到有关图像下载进度的通知,并 无论图像检索是否成功完成:

// 这里我们使用新提供的 setImageWithURL: 方法来加载 网络图片

[cell.imageView setImageWithURL:[NSURL
> URLWithString:@"http://www.domain.com/path/to/image.jpg"]
>                placeholderImage:[UIImage imageNamed:@"placeholder.png"]
>                       completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {... completion code here ...}];

注意:

如果您的图像,您的成功或失败块都不会被调用 请求在完成前被取消。

使用 SDWebImageManager

SDWebImageManager 是 UIImageView+WebCache 后面的类 类别。它将异步下载器与图像缓存联系起来 店铺。您可以直接使用此类从网络图像中受益 在 UIView 之外的另一个上下文中使用缓存下载(即:使用 可可)。

这里有一个如何使用 SDWebImageManager 的简单示例:

SDWebImageManager *manager = [SDWebImageManager sharedManager];
> [manager downloadWithURL:imageURL
>                  options:0
>                  progress:^(NSInteger receivedSize, NSInteger expectedSize)
>                  {
>                      // progression tracking code
>                  }
>                  completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
>                  {
>                      if (image)
>                      {
>                          // do something with image
>                      }
>                  }];

独立使用异步图片下载器

也可以单独使用异步图片下载器:

[SDWebImageDownloader.sharedDownloader downloadImageWithURL:imageURL
>                                                     options:0
>                                                    progress:^(NSInteger receivedSize, NSInteger expectedSize)
>                                                    {
>                                                        // progression tracking code
>                                                    }
>                                                    completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)
>                                                    {
>                                                        if (image && finished)
>                                                        {
>                                                            // do something with image
>                                                        }
>                                                    }];

独立使用异步图像缓存

也可以使用基于 aync 的图像缓存存储 独立。 SDImageCache 维护一个内存缓存和一个可选的 磁盘缓存。磁盘缓存写操作是异步执行的,所以 它不会给 UI 增加不必要的延迟。

为了方便,SDImageCache 类提供了一个单例实例 但如果您想创建单独的实例,您可以创建自己的实例 缓存命名空间。

要查找缓存,请使用 queryDiskCacheForKey:done: 方法。如果 该方法返回 nil,这意味着缓存当前不拥有 图片。因此,您负责生成和缓存它。这 缓存键是要缓存的图像的应用程序唯一标识符。 一般是图片的绝对网址。

 SDImageCache *imageCache = [[SDImageCache alloc]
> initWithNamespace:@"myNamespace"]; [imageCache
> queryDiskCacheForKey:myCacheKey done:^(UIImage *image) {
>     // image is not nil if image was found }];

默认情况下,如果内存中找不到图像,SDImageCache 会查找磁盘缓存

缓存。您可以通过调用替代方法来防止这种情况发生 方法 imageFromMemoryCacheForKey:.

要将图像存储到缓存中,请使用 storeImage:forKey: 方法:

[[SDImageCache sharedImageCache] storeImage:myImage
> forKey:myCacheKey];

默认情况下,图片会存储在内存中

缓存以及磁盘缓存(异步)。如果你只想要 内存缓存,使用替代方法 storeImage:forKey:toDisk: 带有否定的第三个参数。

使用缓存键过滤器

有时,您可能不想使用图像 URL 作为缓存键,因为 URL 的一部分是动态的(即:用于访问控制目的)。 SDWebImageManager 提供了一种设置缓存键过滤器的方法,该过滤器采用 NSURL 作为输入,并输出一个缓存键 NSString。

以下示例在应用程序委托中设置一个过滤器 将在将其用作缓存之前从 URL 中删除任何查询字符串 关键:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
>     SDWebImageManager.sharedManager.cacheKeyFilter:^(NSURL *url)
>     {
>         url = [[[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path] autorelease];
>         return [url absoluteString];
>     };
> 
>     // Your app init code...
>     return YES; }

【讨论】:

  • 如何使用 sdwebimage;其实我不熟悉这个词,我的时间很短
  • 此 github 链接中有可用的文档,您可以研究它。
  • 有没有其他简单或快速的方法来提高它的性能。比如表格不要出列它的单元格并加载所有数据
  • 当您从服务器获取图像时,这是一项繁重的操作,您必须将其称为异步方式,是的,可重用性肯定会帮助您,但它仍然会在第一次加载单元时出现延迟。
猜你喜欢
  • 2021-05-05
  • 1970-01-01
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-23
  • 2018-07-11
相关资源
最近更新 更多