UIGraphicsGetImageFromCurrentImageContext内存泄漏预览

问题描述:

我想在PDF 创建的页面预览图像,但我有一些问题,释放内存。UIGraphicsGetImageFromCurrentImageContext内存泄漏预览

我写了一个简单的测试算法的问题周期, 附近的第40次迭代应用程序崩溃:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"myPdf.pdf"]; 
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, (CFStringRef)pdfPath, kCFURLPOSIXPathStyle, NO); 
CGPDFDocumentRef myPdf = CGPDFDocumentCreateWithURL(url); 
CFRelease (url); 
CGPDFPageRef page = CGPDFDocumentGetPage(myPdf, 1); 

int i=0; 
while(i < 1000){ 

    UIGraphicsBeginImageContext(CGSizeMake(768,1024)); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0); 
    CGContextFillRect(context,CGRectMake(0, 0, 768, 1024)); 
    CGContextSaveGState(context); 
    CGContextTranslateCTM(context, 0.0, 1024); 
    CGContextScaleCTM(context, 1.0, -1.0); 
    CGContextDrawPDFPage(context, page); 
    CGContextRestoreGState(context); 

    // -------------------------- 
    // The problem is here (without this line the application doesn't crash) 
    UIImageView *backgroundImageView1 = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()]; 
    // -------------------------- 

    UIGraphicsEndImageContext(); 
    [backgroundImageView1 release]; 

    NSLog(@"Loop: %d", i++); 
} 

CGPDFDocumentRelease(myPdf); 

上述线似乎然而,以生成存储器泄漏, ,仪器不显示内存问题;

我可以从这种错误的逃生?有人可以解释我以何种方式? 是否有其他方式显示PDF预览?

UPDATE

我觉得问题不是UIImage该方法UIGraphicsGetImageFromCurrentImageContext()UIImageView这个自动释放的形象创造了释放所产生的释放。

我已分割的代码行中的三个步骤:

UIImage *myImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIImageView *myImageView = [[UIImageView alloc] init]; 
[myImageView setImage: myImage]; // Memory Leak 

的第一和第二线不产生内存泄漏所以我认为该方法是UIGraphicsGetImageFromCurrentImageContext不是问题。

我也尝试如下,但问题仍然存在:

UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage]; 

我觉得这是在包含有自动释放属性的UIImage一个UIImageView释放内存泄漏。

我试着写我的对象继承的UIImageView一个UIView如本thread解释。

此解决方案,但不是很优雅,这是一个解决办法,我更愿意使用对象的UIImageView解决内存问题。

+0

你用什么工具检查内存使用情况?我会看看“分配”和“活动监视器”,它们都可以显示泄漏工具漏掉的内存使用情况。另外,这段代码应该做什么呢?你想创建1000个图像视图,但没有使用它们就摆脱它们? – 2011-03-21 19:38:14

+0

你找到答案了吗?我有同样的问题:S – 2013-12-12 23:13:40

这段代码在主线程上运行? UIGraphicsGetImageFromCurrentImageContext(link)的文档说它必须以这种方式运行。

+0

是的,我阅读了Apple文档。我在主线程中调用了这个函数,非常感谢您的回应! – Alessandro 2011-02-26 17:32:54

+8

从UIGraphicsGetImageFromCurrentImageContext的最新UIKit文档:在iOS 4和更高版本中,您可以从您的应用的任何线程调用此函数。 – chown 2012-11-03 16:59:33

问题是这样的:

UIGraphicsGetImageFromCurrentImageContext() 

返回一个自动释放UIImage。 autorelease池保留这个图像,直到你的代码将控制返回到runloop,而你很久没有这样做。为了解决这个问题,你就必须创建并耗尽你while循环在每个迭代上一个新的自动释放池(或每几个迭代)。

+2

我试图在每次迭代中创建并释放NSAutoreleasePool,但内存问题仍然存在。 非常感谢您的回复! – Alessandro 2011-02-26 17:43:47

+0

你有没有得到任何解决方案? – spd 2011-08-26 12:00:17

+0

为我工作。谢谢你的提示! – pstoppani 2012-03-27 18:19:35

你崩溃的线,你可以像下面

得到一个UIImage的退出循环

rendered_image = UIGraphicsGetImageFromCurrentImageContext()来更新它;

我知道这是一个古老的问题,但我刚刚在我的头上撞了几个小时。在我的应用程序中反复调用

UIImage *image = UIGraphicsGetImageFromCurrentImageContext() 

在一个循环中,尽管调用了image = nil,不知道应用程序在释放之前会保留多久的内存,但它肯定足以让我的应用程序获得内存警告,然后崩溃。

我设法最终通过在@autoreleasepool中调用/使用来自UIGraphicsGetImageFromCurrentImageContext()的图像的代码来解决它。所以我有:

@autoreleasepool { 
    UIImage *image = [self imageWithView:_outputImageView]; //create the image 
    [movie addImage:image frameNum:i fps:kFramesPerSec]; //use the image as a frame in movie 
    image = nil; 
} 

希望可以帮助别人。

+0

这就是我的!谢谢 :) – davvilla 2014-06-04 19:50:44