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

查看:40
本文介绍了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);

上面提到的那行似乎产生了内存泄漏,但是,instruments 没有显示内存问题;

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

我认为在包含具有 autorelease 属性的 UIImage 的 UIImageView 的发布中存在内存泄漏.

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

我尝试编写继承 UIView 的对象 UIImageView,如本线程中所述.

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,您很长时间都不会这样做.为了解决这个问题,您必须在 while 循环的每次迭代(或每几次迭代)中创建并排空一个新的自动释放池.

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天全站免登陆