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;
}
이미지를로드하는 방법과 함께 몇 가지 코드를 게시하십시오. – blazejmar
개인적으로 명확한 루틴에 이미지를 명시 적으로 처리하고 있습니다. –
시나리오를 더 잘 설명 할 수있는 코드가 추가되었습니다. 네가 말하는 루틴은 뭐니, 토니? –