我的iPhone应用程序显示一个CollectionView,其中大约有100个图像由标题分隔。图像的大小约为200Kb。在调试导航器中,在滚动CollectionView时,内存增加到1.75 GB。它仍然运行顺畅,但必须有一个原因。
@property (strong, nonatomic) NSArray *picturesMoon;
@property (strong, nonatomic) NSArray *picturesEarth;
@property (strong, nonatomic) NSArray *picturesVenus;
@property (strong, nonatomic) NSArray *picturesMars;
@property (strong, nonatomic) NSDictionary *plistDict;
@property (strong, nonatomic) NSString *picName;
@property (strong, nonatomic) MyCustomCell *myCell;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Milkyway.png"]];
self.plistDict=[[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Praxis" ofType:@"plist"]] objectForKey:@"Universe"];
// Pictures Moon
picturesMoon = [self.plistDict objectForKey:@"Moon"];
// Pictures Earth
picturesEarth = [self.plistDict objectForKey:@"Earth"];
// Pictures Mars
picturesMars = [self.plistDict objectForKey:@"Mars"];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
self.myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
// Sections
switch (indexPath.section) {
case 0:
self.myCell.imageInCell.image = [UIImage imageNamed:[picturesMoon objectAtIndex:indexPath.item]];
break;
case 1:
self.myCell.imageInCell.image = [UIImage imageNamed:[picturesEarth objectAtIndex:indexPath.item]];
break;
case 2:
self.myCell.imageInCell.image = [UIImage imageNamed:[picturesMars objectAtIndex:indexPath.item]];
break;
case 3:
self.myCell.imageInCell.image = [UIImage imageNamed:[picturesVenus objectAtIndex:indexPath.item]];
break;
default:
break;
}
return self.myCell
}
- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
// Header for Sections
HeaderCollectionView *myHeaderView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath];
switch (indexPath.section) {
case 0:
myHeaderView.labelOutlet.text = @"Moon";
break;
case 1:
myHeaderView.labelOutlet.text = @"Earth";
break;
...
...
default:
break
}
return myHeaderView;
}
MyCustomCell只是代码的几行
#import "MyCustomCell.h"
@implementation MyCustomCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
和MyCustomCell的标题只是
#import <UIKit/UIKit.h>
@interface MyCustomCell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UIImageView *imageInCell;
@end
答案 0 :(得分:0)
[UIImage imageNamed:]
缓存图像。大内存占用不一定是个问题,因为如果内存太低,将自动清除缓存。也就是说,如果要避免缓存,可以使用其他方法加载图像,例如:
NSString *imageName = [picturesMars objectAtIndex:indexPath.item];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"PNG"]; // PNG, or whatever file tile you are actually using
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
self.myCell.imageInCell.image = image;