UIGraphicsGetImageFromCurrentImageContext内存泄漏预览 [英] UIGraphicsGetImageFromCurrentImageContext memory leak with previews

查看:247
本文介绍了UIGraphicsGetImageFromCurrentImageContext内存泄漏预览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PDF
创建预览页面图像,但我在内存释放方面遇到了一些问题。

I'm trying to create previews images of pages in a PDF but I have some problems with the release of memory.

我写了一个简单的内容循环问题的测试算法,
应用程序在第40次迭代附近崩溃

I wrote a simple test algorithm that cycles on the problem, the app crashes near the 40th iteration:

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);

上述行似乎会产生内存泄漏,但
乐器没有显示内存问题;

The above-mentioned line seems to generate a memory leak, however, instruments doesn't show memory problems;

我可以摆脱这种错误吗?有人可以用哪种方式解释我?
是否有其他方式显示pdf的预览?

Can I escape from this kind of mistake?someone can explain me in which way? Are there other ways to show previews of a pdf?

更新

我认为问题不是由 UIGraphicsGetImageFromCurrentImageContext()方法创建的 UIImage 的发布,但是发布使用此自动释放映像创建的 UIImageView

I think the problem isn't the release of UIImage created by the method UIGraphicsGetImageFromCurrentImageContext() but the release of UIImageView created with this autorelease image.

我已经将代码行分为三个步骤:

I have divided the line of code in three steps:

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

第一行和第二行不会造成内存泄漏所以我认为该方法UIGraphicsGetImageFromCurrentImageContext不是问题。

The first and second lines doesn't create memory leaks so I think that the method UIGraphicsGetImageFromCurrentImageContext is not the problem.

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

I also tried as follows but the problem persists:

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

我认为UIImageView的发行版中存在内存泄漏,其中包含具有autorelease属性的UIImage 。

I think there is a memory leak in the release of a UIImageView that contains a UIImage with the autorelease property.

我试图编写我的对象UIImageView继承UIView,如下所述 thread

I tried to write my object UIImageView inheriting a UIView as explained in this thread.

这个解决方案有效但不是很优雅,这是一种解决方法,我宁愿使用对象UIImageView解决记忆问题。

This solution works but isn't very elegant, it's a workaround, I would prefer to use the object UIImageView solving the memory problem.

推荐答案

问题是:

UIGraphicsGetImageFromCurrentImageContext()

返回自动释放 UIImage 。自动释放池保留此图像,直到您的代码将控制权返回到runloop,这是您很长时间没有做到的。要解决这个问题,你必须在的每次迭代(或每几次迭代)中创建并排空一个新的自动释放池,同时循环。

returns an autoreleased UIImage. The autorelease pool holds on to this image until your code returns control to the runloop, which you do not do for a long time. To solve this problem, you would have to create and drain a fresh autorelease pool on every iteration (or every few iterations) of your while loop.

这篇关于UIGraphicsGetImageFromCurrentImageContext内存泄漏预览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆