2013-01-11 47 views
0

應用程序在顯示UICollectionView時應該崩潰。它也創建一個低內存崩潰日誌。如何減少UICollectionView中的內存使用量?我在項目中使用ARC。請幫助...(P.S下面是我的代碼)UICollectionView在iPhone 3GS上的iOS 6.0上崩潰

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 
    return [untaggedImagesArray count]; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    @autoreleasepool { 

    static NSString *identifier = @"imageCell"; 
    imageCell *cvc = (imageCell *)[self.iamgesCollectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 
    if(cvc == nil){ 
     NSArray *topLevelObjects = [[NSBundle mainBundle]loadNibNamed:@"imageCell" owner:nil options:nil]; 
     for(id currentObject in topLevelObjects){ 
      if([currentObject isKindOfClass: [imageCell class]]){ 
       cvc = (imageCell*)currentObject; 


       break; 
      } 
     } 
    } 
    NSString *path = [untaggedImagesArray objectAtIndex:indexPath.row]; 
     NSString *path = [untaggedImagesArray objectAtIndex:indexPath.row]; 
    //[self performSelectorInBackground:@selector(getImage:) withObject:path]; 

    UIImage *cache_image = [images_cache objectForKey:path]; 

    if(cache_image) 
    { 
     [cvc.imageView setImage:cache_image]; 
    } 
    else 
    { 
     [self performSelectorInBackground:@selector(getImage:) withObject:path]; 
     //UIImage *img = [UIImage imageWithContentsOfFile:path]; 
     [images_cache setObject:cellImage forKey:path]; 
     [cvc.imageView setImage:cellImage]; 
    } 


    if([preTaggedPaths containsObject:path]){ 
     [cvc.tagLabel setText:[tagStringsDict valueForKey:[NSString stringWithFormat:@"%d",indexPath.row]]]; 
     [cvc setUserInteractionEnabled:NO]; 
    }else{ 
     [cvc.tagLabel setText:@""]; 
     [cvc setUserInteractionEnabled:YES]; 
    } 

     if([selectedImagesPath containsObject:path]){ 
      [cvc.checkImageView setImage:[UIImage imageNamed:@"checkbox_small_selected.png"]]; 
     }else{ 
      [cvc.checkImageView setImage:[UIImage imageNamed:@"checkbox_small_unselected.png"]]; 
     } 

// 
    return cvc; 
    } 
} 



-(void)getImage:(NSString*)path{ 
    cellImage = [UIImage imageWithContentsOfFile:path]; 
} 

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"clicked to select at index%d",indexPath.row); 
    imageCell *cvc = (imageCell*)[self.iamgesCollectionView cellForItemAtIndexPath:indexPath]; 
    NSString *pathString = [untaggedImagesArray objectAtIndex:indexPath.row]; 
    [cvc.checkImageView setImage:[UIImage imageNamed:@"checkbox_small_selected.png"]]; 
    [selectedImagesPath addObject:pathString]; 
} 

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"clicked to DEselect at index%d",indexPath.row); 
    imageCell *cvc = (imageCell*)[self.iamgesCollectionView cellForItemAtIndexPath:indexPath]; 
    NSString *pathString = [untaggedImagesArray objectAtIndex:indexPath.row]; 
    [cvc.checkImageView setImage:[UIImage imageNamed:@"checkbox_small_unselected.png"]]; 
    [selectedImagesPath removeObject:pathString]; 
// [cvc setSelected:NO]; 
} 

-

(void)getImage:(NSString*)path{ 
    //cellImage = [UIImage imageWithContentsOfFile:path]; 
    NSLog(@"image path: %@", path); 
    cellImage = [UIImage imageNamed:path]; 

} 
+0

嘗試更改getImage:方法來加載圖像「[UIImage imageWithContentsOfFile:path];」並告訴我它是否會停止崩潰。 – Pochi

回答

3

你說是墜機是由內存不足引起的!

一個可能的問題是你的圖像加載機制(什麼是你的圖像的尺寸?):

[UIImage imageWithContentsOfFile:path]; 

這將是每次執行在您的收藏視圖中的每個細胞。

[UIImage imageNamed:@"name"]; 

例如緩存圖像,而imageWithContentsOfFile不!

所以UIImage的方法imageNamed:imageWithContentsOfFile做的事情略有不同。 imageNamed將圖像加載到特殊的系統緩存中,然後使用該圖像路徑的未來呼叫將圖像返回緩存,而不是從磁盤重新加載。 imageWithContentsOfFile只是將圖像加載到指定的路徑上,但沒有緩存。對同一圖像多次調用imageWithContentsOfFile將導致內存中出現多個副本。

如果這是您的問題,請嘗試緩存圖像或使用智能方式僅載入當前正在顯示的圖像。

+0

感謝@alex您的suggession ..我應該調整大小(創建thumnails)圖像。 – user1969912

+0

有很多方法可以防止應用程序因內存過載而崩潰。就像我在我的回答中所說的那樣,我會嘗試只加載真正顯示在屏幕上的圖像。進一步嘗試緩存圖片爲你自己(例如NSCache) – Alexander

+0

我已經應用緩存,但仍然不工作後8圖像..我已經編輯我的問題緩存的圖像。 – user1969912

相關問題