【问题标题】:UITableView scrolling not smooth due to processing由于处理,UITableView 滚动不流畅
【发布时间】:2011-06-30 11:53:03
【问题描述】:

我使用 UITableView 在每个表格视图单元格中显示 2 到 3 张图像作为缩略图。我在表格视图中显示的图像尺寸很大。如果我直接使用这些图像而不减小任何尺寸,那么如果我在 TableView 和其他视图控制器之间来回移动,tableview 可能会丢失一些图像。

所以,我想到了减小图像大小的大小缩减代码。下面给出的代码效果很好,之前的问题已修复。但是 tableview 失去了平滑滚动,因为减小尺寸的代码使用了大量的内存。每次显示新的 tableview 单元格时,它都会处理(减小大小)特定 tableview 单元格中的图像。

可能有一个简单的解决方案。提前致谢。

//大小缩减代码

CGSize newSize=CGSizeMake(_new_width, _new_height);
UIGraphicsBeginImageContext( newSize );
[sd._image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
scene_image_preview.image =newImage;

【问题讨论】:

标签: iphone uitableview ios4 uiimage


【解决方案1】:

除了缩小图像之外,您还可以将生成的 newImage 作为文件存储在应用的文档文件夹中。

  NSData *imageData = UIImagePNGRepresentation([UIImage imageWithCGImage:newImage]);
  CGImageRelease(newImage);
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *path = [NSString stringWithFormat:@"%@/%@%d_%d.png",documentsDirectory, prefix, x, y];
  [imageData writeToFile:path atomically:NO];

【讨论】:

  • 正确的解决方案 - 但是,您可以通过告诉 OP 从 :D 获取路径组件的位置来增加此答案的光泽度。
【解决方案2】:

根据您拥有的图像数量,您可以将所有缩小尺寸的图像保存在 NSMutableDictionary 中,这是您的类的属性。

换句话说:

在你的标题中

@property (nonatomic, retain) NSMutableDictionary *sizedImages;

然后在你的 cellForRowAtIndexPath: 代码中

UIImage *sizedImage = [sizedImages objectForKey:[NSNumber numberWithInt:indexPath.row]];  
//Or you could use the filename as the key

if (!sizedImage) {  
        sizedImage = [self sizeImage];(your sizing method)
        [sizedImages setObject:sizedImage forKey:[NSNumber numberWithInt:indexPath.row]];  
}

【讨论】:

    【解决方案3】:

    一些可能的选择

    • 在后台执行大小调整,以免阻塞主线程。
    • 在执行后缓存调整大小的图像,因此第一次只有延迟(这可以与在后台执行此操作结合使用,因此滚动没有延迟)
    • 您正在下载图像吗?如果是这样,请先下载缩略图并显示它。然后,如果选择了图像,则显示放大到全屏的缩略图,这看起来会是块状的,然后在后台下载全尺寸图像并在加载后显示。
    • 如果您没有下载图像,但它们已包含在应用程序中,则只需包含缩略图并改为显示它们

    【讨论】:

      【解决方案4】:

      绘制图像通常需要时间。我认为您可以尝试通过根据需要提供帧大小来将图像放置在 imageView 中。我不会改变图像的外观。

      将图像视图添加到单元格并将图像添加到图像视图

      【讨论】:

        【解决方案5】:

        这两个链接很有用

        http://www.fieryrobot.com/blog/2008/10/01/glassy-scrolling-with-uitableview/

        和他的后续帖子

        http://www.fieryrobot.com/blog/2008/10/08/more-glassy-scrolling-with-uitableview/

        第一个链接是一些最佳实践。第二个链接将对您更有用;将整个表格视图单元格渲染为图像并改用它 - 这可以缓存到磁盘以释放内存并可以在应用程序运行之间使用。

        【讨论】:

          【解决方案6】:
          - (void)saveImage:(UIImage *)image withName:(NSString *)name {
          
                      [myArray addObject:name];
          
                  //save image
                      NSData *data = UIImageJPEGRepresentation(image, 1.0);
                      NSFileManager *fileManager = [NSFileManager defaultManager];
                  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                  NSString *documentsDirectory = [paths objectAtIndex:0];
                      NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
                      [fileManager createFileAtPath:fullPath contents:data attributes:nil];
          
              }
          
              - (UIImage *)loadImage:(NSString *)name {
          
                  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                  NSString *documentsDirectory = [paths objectAtIndex:0];
                      NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];    
                      UIImage *res = [UIImage imageWithContentsOfFile:fullPath];
          
                      return res;
              }
          

          最后,我找到了同时加载大量大尺寸图像的解决方案。首先,我使用上面的尺寸缩小代码缩小了图像的尺寸,我第一次将这些尺寸的图像保存到了文档目录中,然后我使用这些 saveImage 和 loadImage 函数从文档目录中加载了保存的图像。

          感谢所有 cmets 和解决此问题的建议。

          【讨论】:

            猜你喜欢
            • 2014-12-06
            • 2011-11-06
            • 2019-07-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-01-30
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多