UICollectionView の表示中にアプリがクラッシュする可能性があります。メモリ不足のクラッシュ ログも作成します。UICollectionView のメモリ使用量を減らすにはどうすればよいですか? プロジェクトでARCを使用しています。助けてください...(PS以下は私のコードです)
- (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];
}