3

15 개의 요소로 구성된 NSMutableArray가 있는데, 모두 문자열입니다. 이 문자열은 앱에 로컬로 저장된 이미지 이름을 나타냅니다 (약 640x640 이미지). UICollectionView에 이미지 배열을 표시하고 있습니다.큰 이미지의 UICollectionView에 대해 메모리가 해제되지 않습니다.

이미지를 표시하기 위해 collectionView를 다시로드하면 크기가 커서 기대했던 것보다 훨씬 큰 png가 표시되기 때문에 내 메모리 사용량이 늘어납니다.

실제 문제는 이러한 이미지에 사용되는 메모리가 절대로 사용되지 않는다는 것입니다. 따라서 배열에서 모든 객체를 제거하고 collectionView를 제거하고 모든 것을 nil로 설정하더라도 아무 메모리도 해제되지 않습니다.

왜 이런가요? 이러한 객체를 제거하고 ViewController를 제거하면 내 메모리가 원래 수준으로 돌아갈 것으로 예상됩니다.

업데이트 : 코드 스 니펫을

ViewController.m

@implementation ViewController 

static NSString *CellIdentifier = @"photoCell"; 

-(void)viewDidLoad{ 

    [super viewDidLoad]; 

    self.photoCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, 320) collectionViewLayout:self.photoSpringFlowLayout]; 
    [self.photoCollectionView setDataSource:self]; 
    [self.photoCollectionView setDelegate:self]; 
    [self.view addSubview:self.photoCollectionView]; 
    [self.photoCollectionView registerClass:[PhotoPreviewCell class] forCellWithReuseIdentifier:CellIdentifier]; 

    self.imagesAray = [[NSMutableArray alloc] initWithObjects:@"photo1", @"photo2", @"photo3", @"photo4", @"photo5", @"photo6", @"photo7", @"photo8", @"photo9", @"photo10", @"photo11", @"photo12", @"photo13", @"photo14", @"photo15", nil]; 

    [self.photoCollectionView reloadData]; 
} 


#pragma mark - UICollectionView Methods 

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ 
    return 1; 
} 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 
    return self.imagesArray.count; 
} 

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

    PhotoPreviewCell *cell = (PhotoPreviewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; 

    NSString *imageName = self.imagesArray[indexPath.row]; 
    [cell.photoImage setImage:[UIImage imageNamed:imageName]]; 

    return cell; 

} 

PhotoPreviewCell.m

@implementation PhotoPreviewCell 

-(id)initWithFrame:(CGRect)frame{ 

    self = [super initWithFrame:frame]; 

    if(self){ 

     self.photoImage = [[UIImageView alloc] init]; 
     self.photoImage.frame = CGRectMake(0, 0, 160, 160); 
     self.photoImage.contentMode = UIViewContentModeScaleAspectFit; 
     [self.contentView addSubview:self.photoImage]; 

    } 

    return self; 
} 
+0

이미지를로드하는 방법과 함께 몇 가지 코드를 게시하십시오. – blazejmar

+0

개인적으로 명확한 루틴에 이미지를 명시 적으로 처리하고 있습니다. –

+0

시나리오를 더 잘 설명 할 수있는 코드가 추가되었습니다. 네가 말하는 루틴은 뭐니, 토니? –

답변

2

그 이유는 플래시에서 이미지로드가 약간의 시간과 OS 시도를 소요 그래서 확인 그것을 계속로드하는 것을 피하기 위해로드 된 이미지를 캐싱합니다. 그들은 imageNamed: 메소드를 사용할 때만 캐시됩니다. 시뮬레이터에서 테스트 중이라면 모든 객체를 릴리스하고 메모리 경고를 시뮬레이트 해보십시오. 이것은 OS가 캐시에서 사용되지 않은 이미지를 제거하도록 강제해야합니다.

+0

예, 맞습니다. imageNamed : 문제의 원인입니다. imageNamed : 이미지를 캐시하고 메모리를 제어하지 못합니다 (이미지를 캐시하지 않는 imageWithContentsOfFile과는 다릅니다). 따라서 OS가 캐시를 비울 때 (예 : 응용 프로그램이 메모리 부족 경고를받을 때), 메모리 경고를 받으면 OS가 캐시를 비우기 때문에 메모리 사용량이 증가하지 않도록해야합니다. 그게 올바른 가정입니까? –

+0

이러한 이미지를 모두로드해도 메모리 사용 공간이 충분하지 않으면 확인해야합니다. 번들에 포함 시키면 크기를 조정하지 않고도 표시 할 수있는 크기로 작게 유지하면 이진 크기와 메모리 사용 공간이 줄어 듭니다. – blazejmar

관련 문제