我在使用PFImages的自定义集合视图时遇到问题占用太多内存。在Parse中,我只有7个对象,但在加载集合视图时内存最多可达1.1千兆字节。此外,离开视图时内存不会解除分配。
这是我的代码
自定义单元格
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)anotherDecoder {
self = [super initWithCoder:anotherDecoder];
if (self) {
[self setup];
}
return self;
}
- (void)setup {
self.pictureImageView = [[PFImageView alloc]init];
self.pictureImageView.frame = CGRectMake(0, 0, self.pictureView.frame.size.width, self.pictureView.frame.size.height);
[self.contentView addSubview:pictureImageView];
}
收藏展示代码
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
PicturesCollectionViewCell *cell = (PicturesCollectionViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
tempObject = [picturesArray objectAtIndex:indexPath.row];
PFFile *file = [tempObject objectForKey:@"thumbnail"];
cell.pictureImageView.file = file;
cell.pictureImageView.image = [UIImage imageNamed: @"LOADING.png"];
[cell.pictureImageView loadInBackground];
cell.pictureImageView.contentMode = UIViewContentModeScaleAspectFill;
cell.titleLabel.text = [tempObject objectForKey:@"title"];
NSLog(@"Image: %@, File: %@, at index: %li", cell.pictureImageView.image, cell.pictureImageView.file, indexPath.row);
return cell;
}
答案 0 :(得分:0)
Parse在这种情况下非常糟糕......代码会在收到内存警告时触发清除,这有时会有所帮助:
- (void)removeCacheFiles {
[PFQuery clearAllCachedResults];
NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(
NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *nsurlDir =
[[NSString alloc] initWithFormat:@"%@/YOUR_BUNDLE_ID", cacheDir];
NSFileManager *manager = [NSFileManager defaultManager];
// grab all the files in the documents dir
NSArray *allFiles = [manager contentsOfDirectoryAtPath:nsurlDir error:nil];
// filter the array for only sqlite files
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.db'"];
NSArray *dbFiles = [allFiles filteredArrayUsingPredicate:fltr];
// use fast enumeration to iterate the array and delete the files
for (NSString *dbFile in dbFiles) {
NSError *error = nil;
[manager removeItemAtPath:[nsurlDir stringByAppendingPathComponent:dbFile]
error:&error];
if (error != nil) {
NSLog(@"Error:%@", [error description]);
} else {
NSLog(@"DB FILE Cleared: %@", dbFile);
}
}
}