[英]UITableView not scrolling smooth due to removeFromSuperview
[英]UITableView scrolling not smooth due to processing
我正在使用 UITableView 在每個表格視圖單元格中顯示 2 到 3 張圖像作為縮略圖。 我在表格視圖中顯示的圖像尺寸很大。 如果我直接使用這些圖像而不減小任何尺寸,那么如果我在 TableView 和其他視圖控制器之間來回移動,tableview 可能會丟失一些圖像。
所以,我想到了減小圖像大小的縮小代碼。 下面給出的代碼效果很好,之前的問題已修復。 但是tableview失去了平滑滾動,因為縮小尺寸的代碼使用了大量的memory。 每次顯示新的 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;
除了縮小圖像之外,您還可以將生成的 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];
根據您擁有的圖像數量,您可以將所有縮小尺寸的圖像保存在 NSMutableDictionary 中,這是您的 class 的屬性。
換句話說:
在你的 header
@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]];
}
一些可能的選擇
繪制圖像通常需要時間。我認為您可以通過根據需要提供幀大小來嘗試將圖像放置在 imageView 中。我對圖像的外觀沒有影響。
將圖像視圖添加到單元格並將圖像添加到 imageView
這兩個鏈接非常有用
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/
第一個鏈接是一些最佳實踐。 第二個鏈接將對您更有用; 將整個表格視圖單元格渲染為圖像並改用它 - 這可以緩存到磁盤以釋放 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;
}
最后,我得到了一個同時加載大量大尺寸圖像的解決方案。 首先,我使用上面的尺寸縮小代碼縮小了圖像的大小,我第一次將這些尺寸的圖像保存到文檔目錄,然后我使用這些 saveImage 和 loadImage 函數從文檔目錄加載保存的圖像。
感謝所有解決此問題的意見和建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.