Ios5 从核心数据加载大型图像,10分钟后内存使用率会出现峰值

Ios5 从核心数据加载大型图像,10分钟后内存使用率会出现峰值,ios5,core-data,Ios5,Core Data,在我的iOS应用程序中,我有一个启用分页的UIScrollView。每个页面显示一个视图(具有多个子视图);任何时候只加载3个“页面”——当前查看的页面、左侧的页面和右侧的页面。加载是在后台惰性地完成的。应用程序正在使用ARC 每个页面上的视图主要由从核心数据检索的图像组成。此图像可能较大,因此首先加载缩略图,然后替换为较大的图像。这个较大的图像实际上是数据存储中实际内容的缩小版本-对于不同的屏幕需要全分辨率版本,但是对于这个图像滚动器,它只需要适合页面。实际存储的图像可能要大得多(2448x3

在我的iOS应用程序中,我有一个启用分页的UIScrollView。每个页面显示一个视图(具有多个子视图);任何时候只加载3个“页面”——当前查看的页面、左侧的页面和右侧的页面。加载是在后台惰性地完成的。应用程序正在使用ARC

每个页面上的视图主要由从核心数据检索的图像组成。此图像可能较大,因此首先加载缩略图,然后替换为较大的图像。这个较大的图像实际上是数据存储中实际内容的缩小版本-对于不同的屏幕需要全分辨率版本,但是对于这个图像滚动器,它只需要适合页面。实际存储的图像可能要大得多(2448x326用于照相机的照片)。注意,Core Data中的image属性设置为允许外部存储,因此在大多数情况下,它不存储在SQLite数据库中

一切都“工作”良好:滚动条和图像加载速度很快(首先是缩略图,然后是较大的图像),滚动速度很快。根据仪器,内存使用情况也很好——直到第11张图像加载时,内存峰值约为5MB;加载的后续图像可能会导致更多的内存峰值(不是每一个,可能每一个都会导致另一个~5MB的峰值)。没有特定的图像导致峰值(我改变了加载图像的顺序,总是第11个)。这种记忆似乎永远不会被释放

下面是在后台加载图像的代码片段:

- (void)loadImageWithCardInstanceObjectId:(NSManagedObjectID *)cardInstanceObjectId
                           imageIndex:(NSUInteger)imageIndex
              withInitialImageHandler:(void (^)(UIImage *image))initialImageHandler
                withFinalImageHandler:(void (^)(UIImage *image))finalImageHandler
{
    NSManagedObjectContext *tempMoc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    tempMoc.parentContext = [[DataManager sharedInstance] managedObjectContext];

    [tempMoc performBlock:^{
        CardInstance *ci = (CardInstance *)[tempMoc objectWithID:cardInstanceObjectId];
        if (ci.cardInstanceImages.count == 0) {
            // no card images, return the default image
            dispatch_async(dispatch_get_main_queue(), ^{
                initialImageHandler([UIImage imageNamed:@"CardNoImage.png"]);
            });
            return;
        }

        // have card images, pick the one according to the passed in index
        UIImage *thumbnail = [[ci.cardInstanceImages objectAtIndex:imageIndex] thumbnailRepresentation];
        dispatch_async(dispatch_get_main_queue(), ^{
            initialImageHandler(thumbnail);
        });

        // THIS IS THE OFFENDING LINE
        UIImage *fullImage = [[ci.cardInstanceImages objectAtIndex:imageIndex] imageRepresentation];

        CGSize size = fullImage.size;
        CGFloat ratio = 0;
        if (size.width > size.height) {
            ratio = 240.0 / size.width;
        }
        else {
            ratio = 240.0 / size.height;
        }
        CGRect rect = CGRectMake(0.0, 0.0, ceilf(ratio * size.width), ceilf(ratio * size.height));

        // create the image to display
        UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0);
        [fullImage drawInRect:rect];
        UIImage *imageToDisplay = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        dispatch_async(dispatch_get_main_queue(), ^{
            finalImageHandler(imageToDisplay);
        });
    }];
}
在instruments中,负责分配的库/调用方是CoreData+[\u PFRoutines ReadExternalReferencedDataFromFile:]


有没有想过为什么会出现这种记忆尖峰?谢谢

模拟内存警告时,内存大小的响应是什么?