简体   繁体   English

UITableView 由于处理滚动不流畅

[英]UITableView scrolling not smooth due to processing

I'm using a UITableView to show 2 to 3 images as thumbnail in each table view cell.我正在使用 UITableView 在每个表格视图单元格中显示 2 到 3 张图像作为缩略图。 The images which I'm showing it in the tableview are huge in size.我在表格视图中显示的图像尺寸很大。 If I'm using those images directly without any size reduction then the tableview may lose some images if I move back and forth between TableView and other view controllers.如果我直接使用这些图像而不减小任何尺寸,那么如果我在 TableView 和其他视图控制器之间来回移动,tableview 可能会丢失一些图像。

So, I thought of size reduction code which reduces the size of the images.所以,我想到了减小图像大小的缩小代码。 The code given below works great and the previous issue was fixed.下面给出的代码效果很好,之前的问题已修复。 But the tableview looses its smooth scrolling because the size reduction code uses a lot of memory.但是tableview失去了平滑滚动,因为缩小尺寸的代码使用了大量的memory。 Every time when a new tableview cell is shown, it processes (reduces the size) the images in the particular tableview cell.每次显示新的 tableview 单元格时,它都会处理(减小大小)特定 tableview 单元格中的图像。

There may be a simple solution for this.可能有一个简单的解决方案。 Thanks in Advance.提前致谢。

//Size Reduction Code //尺寸缩小代码

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;

On top of reducing the image you can store the resulting newImage as a file in the documents folder of your app.除了缩小图像之外,您还可以将生成的 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];

Depending on how many images you have, you can just keep all the reduced-size images in an NSMutableDictionary that's a property of your class.根据您拥有的图像数量,您可以将所有缩小尺寸的图像保存在 NSMutableDictionary 中,这是您的 class 的属性。

In other words:换句话说:

In your header在你的 header

@property (nonatomic, retain) NSMutableDictionary *sizedImages;

Then in your cellForRowAtIndexPath: code然后在你的 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]];  
}

A few possible options一些可能的选择

  • Perform the resizing in the background so it doesn't hold up the main thread.在后台执行大小调整,以免阻塞主线程。
  • Cache the resized image after doing it so there is only a delay the first time (this could be used in conjunction with doing it in the background so there is no delay on the scrolling)完成后缓存调整大小的图像,因此第一次只有延迟(这可以与在后台执行此操作结合使用,因此滚动没有延迟)
  • Are you downloading the images?你是在下载图片吗? If so, download a thumbnail first and display that.如果是这样,请先下载缩略图并显示它。 Then if the image is selected, display the thumbnail scaled up to full screen which will look blocky, but then download the full-size image in the background and display that when it has loaded.然后,如果选择了图像,则显示放大到全屏的缩略图,这看起来会是块状的,然后在后台下载全尺寸图像并在加载后显示。
  • If you aren't downloading the images, but they are included in the app, just include thumbnails as well and display them instead如果您没有下载图像,但它们已包含在应用程序中,则只需包含缩略图并显示它们即可

drawing images usually takes time.i think you can try to place the images in imageView by giving the frame sizes as desired.i doesn't make difference in look of the images.绘制图像通常需要时间。我认为您可以通过根据需要提供帧大小来尝试将图像放置在 imageView 中。我对图像的外观没有影响。

add the imageviews to the cell and add the images to the imageView将图像视图添加到单元格并将图像添加到 imageView

These two links are very useful这两个链接非常有用

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

and his follow up post和他的后续帖子

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

The first link is a few best practices.第一个链接是一些最佳实践。 The second link will eb more useful to you;第二个链接将对您更有用; render your whole table view cell to an image and use that instead - this can be cached to disk to free memory & can be used between runs of the app.将整个表格视图单元格渲染为图像并改用它 - 这可以缓存到磁盘以释放 memory 并且可以在应用程序运行之间使用。

- (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;
    }

Finally, I got a solution to load number of large sized images simultaneously.最后,我得到了一个同时加载大量大尺寸图像的解决方案。 First, I reduced the size of the image using the size reduction code above and I saved those sized images to documents directory for the first time and then I loaded the saved images from the documents directory using these saveImage and loadImage functions.首先,我使用上面的尺寸缩小代码缩小了图像的大小,我第一次将这些尺寸的图像保存到文档目录,然后我使用这些 saveImage 和 loadImage 函数从文档目录加载保存的图像。

Thanks for all the comments and suggestions to solve this.感谢所有解决此问题的意见和建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM