2011-06-30 68 views
0

我使用UITableView在每個表視圖單元格中顯示2到3個圖像作爲縮略圖。我在桌面視圖中顯示的圖像尺寸很大。如果我直接使用這些圖像而不減小任何尺寸,那麼如果我在TableView和其他視圖控制器之間來回移動,tableview可能會丟失一些圖像。由於處理UITableView滾動不平滑

所以,我想到縮小圖像尺寸的縮小代碼。下面給出的代碼很好,前一個問題已修復。但是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; 

回答

0
- (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函數從文檔目錄加載保存的圖像。

感謝所有的意見和建議,以解決這個問題。

3

在減少,你可以得到的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]; 
+0

正確的解決方案 - 您可以通過告訴OP從何處獲取路徑組件來增加此答案的亮度。 – Till

+0

@Till - 感謝提示,我更新了代碼示例。 – werner

0

有幾個可能的選項

  • 所以它不撐起主線程在後臺執行調整大小。
  • 這樣做後緩存大小調整的圖像,所以只有第一次延遲(這可以在背景中使用,所以沒有延遲滾動)
  • 你下載的圖像?如果是這樣,請首先下載縮略圖並顯示該縮略圖。然後,如果選擇了圖像,則顯示縮放至縮略圖的縮略圖,然後在後臺下載全尺寸圖像,並在加載後顯示。
  • 如果你不下載圖像,但它們包含在應用程序中,只包括縮略圖,以及和顯示他們,而不是
0

繪製圖像通常需要再寄一次想你可以嘗試把圖像在imageView中通過給出所需的幀尺寸,在圖像外觀上沒有區別。

的imageviews添加到細胞,並添加圖片到ImageView的

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