2012-07-19 85 views
0

我有這樣的代碼和分析的XCode沒有發出任何警告,但在控制檯每一次我得到一個消息:麻煩與CALayer的

2012-07-19 23:15:35.122 AttachIt [5725:907]收到內存警告。

請問我的錯在哪裏?

for(int j=0;j<images;j++){ 
    @autoreleasepool { 
     NSInteger currentRow = 0; 

     for(int k = 0; k<i;k++) 
      currentRow = currentRow + [[assetGroups objectAtIndex:k] numberOfAssets]; 

     asset = [assets objectAtIndex:j+currentRow]; 
     float size = [self getRandomNumberBetweenMin:60.0 andMax:65.0]; 
     CGRect rect; 
     if(iPad) 
      rect = CGRectMake(10+j*35.5, 75-size, size, size); 
     else 
      rect = CGRectMake(10+j*26, 75-size, size, size); 
     UIImageView *temp = [[UIImageView alloc] initWithImage:[UIImage imageWithCGImage:[asset thumbnail]]]; 
     temp.frame = rect; 
     temp.layer.backgroundColor = [UIColor blueColor].CGColor; 
     temp.layer.shadowOffset = CGSizeMake(0, 3); 
     temp.layer.shadowRadius = 5.0; 
     temp.layer.shadowColor = [UIColor blackColor].CGColor; 
     temp.layer.shadowOpacity = 0.8; 
     temp.layer.masksToBounds = NO; 
     temp.layer.borderColor = [[UIColor whiteColor] CGColor]; 
     temp.layer.borderWidth = 2; 
     [temp setTransform:CGAffineTransformMakeRotation(degreesToRadians([self getRandomNumberBetweenMin:-5 andMax:5]))]; 
     temp.layer.shouldRasterize = TRUE; 
     [albumRow addSubview:temp]; 
    } 
} 
+0

圖像有多大? – 2012-07-19 19:51:56

+0

75x75(資源縮略圖) – Torokh 2012-07-19 19:52:59

+0

「圖像」變量的值是多少? – 2012-07-19 20:00:25

回答

1

內存警告不是錯誤的結果;它們是在iOS上運行的正常部分。你正在分配一堆東西,所以得到一個內存警告並不奇怪。

如果您的應用程序由於內存過量使用而被系統殺死,您可能會考慮隨着時間而不是循環填充這些圖像。你可以通過一個NSOperationQueue或類似的機制來完成這個任務,一次加載一個圖像。

在任何情況下,您都可以使用「分配」或「活動監視器」工具來查看您的內存使用量是否在增長以及多少。

+0

謝謝。也許你可以告訴我如何在運行下一個功能之前運行功能並等待他的完成。 'dispatch_async(dispatch_get_main_queue(),^ {activity.hidden = false;});'不適用於我的情況 – Torokh 2012-07-19 23:29:59

+0

我不確定你的意思。我認爲你上面的代碼對於少量圖像看起來很好。對於大量數據,您可能希望在後臺線程上加載資源,然後將它們異步傳遞到UI,而不是在主線程上阻塞。 – 2012-07-20 00:08:08