2011-12-20 114 views
0

我正在嘗試在iOS中創建像Apple照片應用程序一樣的照片查看器。 佈局是好的,但它收到內存警告,然後崩潰。爲什麼?即使我從應用程序文檔文件夾加載7/8圖像,也會發生這種情況。我有沒有用特定的系統管理內存?我用ARC與iOS 5.iOS - 創建像照片應用程序的照片查看器

編輯:

的代碼:

for (int i=0; i<[dataSource count]; i++) { 
     UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom]; 
     [button setImage:[dataSource objectAtIndex:i] forState:UIControlStateNormal]; 
     [[button titleLabel] setText:[NSString stringWithFormat:@"%i",i+1]]; 
     [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [[button layer] setBorderWidth:1]; 
     [[button layer] setBorderColor:[UIColor darkGrayColor].CGColor]; 
     if (i==0) { 
      [button setFrame:CGRectMake(x, y, width, height)]; 
     } else { 
      if (i%5==0) { 
       nRow++; 
       x=18; 
       [button setFrame:CGRectMake(x, (y*nRow), width, height)]; 
      } else { 
       [button setFrame:CGRectMake(x+space+width, (y*nRow), width, height)]; 
       x=button.frame.origin.x; 
      } 
     } 
     [[self view] addSubview:button]; 
    } 

此代碼的主要部分是前6行,之後是所有的x和y。 dataSource是一個聲明爲屬性(非原子,強大)的NSArray。它包含UIImage對象。

+0

@ user523234代碼添加 – 2011-12-20 15:40:12

+0

@ user523234簡單的接收內存警告,然後崩潰 – 2011-12-21 10:44:53

回答

0

你應該懶洋洋地加載在你的圖片結合重新使用按鈕來說明大量圖像的可能性。

要實現:

  1. 保持路徑的圖像文件數據陣列,而不是UIImage的對象英寸使用imageWithContentsOfFile從路徑中獲取圖像:當你需要它時。
  2. 將第一個z按鈕加載到滾動視圖中,其中z是一次顯示在屏幕上的數字加上一行的值。
  3. 將我們當前所在的UIViewController設置爲scrollview的委託,並通過重新定位按鈕並設置適當的圖像和目標來響應偏移中的更改。

此外,如果7/8圖像崩潰你的應用程序,這聽起來像你正在處理一些非常大的圖像文件。嘗試在文檔目錄中提供縮略圖大小的版本(無論您的按鈕的大小是否正確),或者圖像是動態的,請參閱this post瞭解操作方法。

0

如果您使用的可能ImageNamed,這篇文章幫助我很多:

http://www.alexcurylo.com/blog/2009/01/13/imagenamed-is-evil/

主要

不要用[UIImage的imageNamed對於圖像的任何顯著量。這是EVIL。它會降低你的應用程序和/或Springboard,即使你的應用程序只使用了幾乎一小撮的內存。

這是更好地實現自己的高速緩存

,這裏是所提出的緩存形象的例子:

- (UIImage*)thumbnailImage:(NSString*)fileName 
{ 
    UIImage *thumbnail = [thumbnailCache objectForKey:fileName]; 

    if (nil == thumbnail) 
    { 
     NSString *thumbnailFile = [NSString stringWithFormat:@"%@/thumbnails/%@.jpg", [[NSBundle mainBundle] resourcePath], fileName]; 
     thumbnail = [UIImage imageWithContentsOfFile:thumbnailFile]; 
     [thumbnailCache setObject:thumbnail forKey:fileName]; 
    } 
    return thumbnail; 
} 
+0

我不使用imageNamed – 2011-12-20 15:40:31

+0

@BartłomiejSemańczyk感謝您的提示,我已經更新了我的答案 – 321zeno 2016-02-03 12:44:00

相關問題