我已经为iPhone / iPad编程了三年了,我遇到了最令人费解的问题。我希望有人能够帮助我,因为我真的很想找到一个解决方案,或者至少帮助我获得一些我必须缺乏的知识。我做了搜索,搜索和研究。 因此,为了演示我的问题,我创建了一个小的简单测试应用程序,其中包含保存在核心数据(名字,姓氏和主要照片)中的主要记录列表。与每个记录相关联的是相册女巫可以包括许多照片。我在这里发布的代码部分是关于我需要对这些照片中的每一个进行的过程,首先将它们从核心数据中的NSData表示转换为字符串。 我的问题是在下一张照片被处理之前应该释放局部变量“stringPhoto”。当我检查调试器的内存分析器时,Witch似乎并不是这样:它会继续增长和增长,与正在处理的照片数量成比例。 由于ARC,“release”和“dealloc”方法不起作用,使用“stringPhoto = nil”根本没有效果。我不想关闭ARC因为我的真实应用程序太大而无法开始管理所有内存使用情况。
谢谢
- (void) convertPhotosToStrings
{
NSManagedObjectContext *managedObjectContext = MOC();
NSFetchRequest *fr = [NSFetchRequest fetchRequestWithEntityName:@"Photos"];
fr.predicate = [NSPredicate predicateWithFormat:@"relatedRecord = %@", self.recordChosen];
fr.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"idPhoto"
ascending:YES]];
self.frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fr managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
self.frc.delegate = self;
NSError *error = nil;
[self.frc performFetch:&error];
if (error) NSLog (@"%@",error);
for (long int currentItem = 0; (currentItem <= [[self.frc fetchedObjects] count]-1);currentItem++)
{
[self convertCurrentPhotoToStringFromCurrentItem : currentItem];
}
}
- (void) convertCurrentPhotoToStringFromCurrentItem : (long int) currentItem
{
NSLog(@"currentItem = %li", currentItem);
sleep (1); //Allows to chech the memory used by each photo
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:currentItem inSection:0];
Photos *currentPhoto = [self.frc objectAtIndexPath:indexPath];
NSString *stringPhoto = [UIImageJPEGRepresentation([UIImage imageWithData:currentPhoto.photoThumbnail],1.0) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
// some process
}
我怀疑在循环结束时程序突然释放所有额外的内存。您需要耗尽自动释放池:
for (long int currentItem = 0; (currentItem <= [[self.frc fetchedObjects] count]-1);currentItem++)
{
@autoreleasepool {
[self convertCurrentPhotoToStringFromCurrentItem : currentItem];
}
}