2013-01-11 2 views
0

UICollectionView를 표시하는 중에 응용 프로그램이 충돌하는 것 같습니다. 낮은 메모리 크래시 로그도 생성합니다. UICollectionView에서 메모리 사용을 어떻게 줄일 수 있습니까? 프로젝트에서 ARC를 사용하고 있습니다.iPhone 3GS의 iOS 6.0에서 UICollectionView가 충돌 함

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

} 
+0

"[UIImage imageWithContentsOfFile : path];"로 이미지를로드하려면 getImage : 메소드를 변경하십시오. 충돌이 멈췄는지 말해봐. – Pochi

답변

3

귀하가 충돌이 메모리 부족에 의해 발생되는 말 - 도와주세요 ...

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

(. P.S은 아래에있는 내 코드입니다)!

가능한 문제는 (이미지의 크기 무엇입니까?) 이미지 로딩 메커니즘 :

[UIImage imageWithContentsOfFile:path]; 

이 컬렉션 뷰에서 각 셀마다 실행됩니다.

[UIImage imageNamed:@"name"]; 

예를 들어 imageWithContentsOfFile은 이미지를 캐싱하지 않습니다.

따라서 UIImage의 방법 imageNamed:imageWithContentsOfFile은 약간 다른 작업을 수행합니다. imageNamed은 특수한 시스템 캐시에 이미지를로드하고 그 이미지 경로를 가진 이후의 호출은 디스크에서 이미지를 다시로드하는 대신 캐시의 이미지를 반환합니다. imageWithContentsOfFile은 지정한 경로의 이미지를 단순히로드하지만 캐싱은 수행하지 않습니다. 동일한 이미지에 대해 imageWithContentsOfFile을 여러 번 호출하면 메모리에 여러 복사본이 생깁니다.

이것이 문제가되면 이미지를 캐싱하거나 현명한 방법으로 현재 표시되는 이미지 만로드하십시오.

+0

당신의 고집을 위해 @alex 주셔서 감사합니다. 나는 (thumnails 생성) 이미지의 크기를 조정해야합니까? – user1969912

+0

메모리 과부하로 인해 앱이 충돌하는 것을 방지하는 여러 가지 방법이 있습니다. 내 답변에서 말했듯이, 나는 화면에 실제로 표시되는 이미지 만로드하려고합니다. 추가로 캐시를 통해 이미지를 캐어 (예 : NSCache) – Alexander

+0

캐시를 적용했지만 여전히 8 개의 이미지를 처리하지 못했습니다. 이미지를 캐싱하여 내 질문을 편집했습니다. – user1969912

관련 문제