3

我花了很多时间试图弄清楚为什么我的应用程序中出现 ImageIO 泄漏......如果有人可以帮助我解决这个问题,我将不胜感激!

我有一个 UIPageViewController,其中包含许多带有 2048x1536 图像的 UIViewController。我将 UIImage 直接设置为 view.layer.contents 为最佳。当用户在图像之间滑动时,我将它们懒惰地加载到 bg 线程上,然后缓存它们。我有一个最大的缓存大小,所以在某些时候,当用户继续滑动时,应用程序开始卸载和重新加载图像。一切正常,但如果我开始快速滑动 2 或 3 个周期,我最终会在调用 CGContextDrawImage 时在 ImageIO 内发生内存泄漏。泄漏仪器内部不报告泄漏。我真的不明白为什么会这样。这是我的代码:

...
Some code here check if there is already a running operation to load imagePath
If so just add the completion block to the completion block list and return

 NSBlockOperation* loadImageOperation = [[NSBlockOperation alloc] init];

// Make a weak reference to avoid a retain cycle
__weak NSBlockOperation* weakOp = loadImageOperation;

[loadImageOperation addExecutionBlock: ^{

    NSLog(@"Loading img %@",[imagePath lastPathComponent]);

    // So this can be executed concurrently several times if the user swipes fast
    UIImage* image = [self loadImageFileForCoreAnimation:imagePath];

    dispatch_async(dispatch_get_main_queue(), ^{

        if (image)
        {
            [self cacheImage:image withKey:imagePath];

            if (completion)
                completion(image);
        }
        else if (!weakOp.isCancelled)
        {
            NSLog(@"Error loading image %@", imagePath);
        }

        [self.runningOperationObjects removeObjectForKey:imagePath];
    });
}];

...

- (UIImage*)loadImageFileForCoreAnimation:(NSString*)imageFile

{ __weak NSOperation* weakRunningOp = [self getRunningOperationWithImagePath:imageFile];

CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename([imageFile UTF8String]);

CGImageRef image = CGImageCreateWithPNGDataProvider(dataProvider, NULL, NO, kCGRenderingIntentDefault);

if(!image)
    image = CGImageCreateWithJPEGDataProvider(dataProvider, NULL, NO, kCGRenderingIntentDefault);

CGDataProviderRelease(dataProvider);

if (weakRunningOp.isCancelled)
{
    CGImageRelease(image);
    return nil;
}

// Here we define the ARGB pixel format
CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little;
size_t width = CGImageGetWidth(image);
size_t height = CGImageGetHeight(image);
size_t bytesPerPixel = 4;
size_t bitsPerComponent = 8;
// bytesPerRow must be at least (width * bytesPerPixel) bytes;
size_t bytesPerRow = width * bytesPerPixel;
// Also : bytesPerRow must be an integer multiple of bytesPerPixel
bytesPerRow = ((bytesPerRow + (bytesPerPixel - 1)) / bytesPerPixel) * bytesPerPixel;

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef imageContext = CGBitmapContextCreate(NULL, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo);
  CGColorSpaceRelease(colorSpace);

if (weakRunningOp.isCancelled)
{
    CGImageRelease(image);
    CGContextRelease(imageContext);
    return nil;
}

// *** This function sometimes leaks!
CGContextDrawImage(imageContext, CGRectMake(0, 0, width, height), image);

CGImageRelease(image);

if (weakRunningOp.isCancelled)
{
    CGContextRelease(imageContext);
    return nil;
}

CGImageRef outputImage = CGBitmapContextCreateImage(imageContext);

UIImage* bitmapImage = [UIImage imageWithCGImage:outputImage scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];

CGImageRelease(outputImage);
CGContextRelease(imageContext);

return bitmapImage;

}

这是我的仪器屏幕截图,在某些时候,VM : ImageIO_PNG_Data 以 12mo 分配出现并且永远不会消失。

在此处输入图像描述

在此处输入图像描述

4

0 回答 0