ARC不会发布CALayer

时间:2014-02-26 15:26:19

标签: ios objective-c memory automatic-ref-counting

我有一个代码块,旨在为每个页面捕获基于pdf的自定义视图的快照。为了实现它,我将在循环中创建视图控制器,然后进行迭代。问题是甚至视图控制器发布的自定义视图没有发布,看起来像是工具上的实时工具。因此循环迭代很多,所以它打破了内存(42个生活高达500MB)和崩溃。 这是迭代代码;

do
    {
        __pageDictionary = CFDictionaryGetValue(_allPages,
                                                __pageID);
        CUIPageViewController *__pageViewController = [self _pageWithID:__pageID];
        [__pageViewController addMainLayers];
        [[APP_DELEGATE assetManager] temporarilyPasteSnapshotSource:__pageViewController.view];
        UIImage *__snapshotImage = [__pageViewController captureSnapshot];
        [[AOAssetManager sharedManager] saveImage:__snapshotImage
                             forPublicationBundle:_publicationTileViewController.publication.bundle
                                           pageID:(__bridge NSString *)__pageID];
        [[APP_DELEGATE assetManager] removePastedSnapshotSource:__pageViewController.view];
                __snapshotImage = nil;
        __pageViewController = nil;

        ind += 6 * 0.1 / CFDictionaryGetCount(_allPages);
    }
    while (![(__bridge NSString *)(__pageID = CFDictionaryGetValue(__pageDictionary,
                                                                   kMFMarkupKeyPageNextPageID)) isMemberOfClass:[NSNull class]]);
    _generatingSnapshots = NO;

这里是captureSnapshot方法;

- (UIImage *)captureSnapshot
{
    CGRect rect = [self.view bounds];
    UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
    context = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:context];
    UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return capturedImage;
}

仪器;

enter image description here

编辑以获取更多详细信息:

下面的代码来自CUIPDFView,是UIView的子类;

- (void)drawRect:(CGRect)rect
{
    [self drawInContext:UIGraphicsGetCurrentContext()];
}

-(void)drawInContext:(CGContextRef)context
{

    CGRect drawRect = CGRectMake(self.bounds.origin.x, self.bounds.origin.y,self.bounds.size.width, self.bounds.size.height);

    CGContextSetRGBFillColor(context, 1.0000, 1.0000, 1.0000, 1.0f);
    CGContextFillRect(context, drawRect);

    // PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system
    // before we start drawing.
    CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // Grab the first PDF page
    CGPDFPageRef page = CGPDFDocumentGetPage(_pdfDocument, _pageNumberToUse);
    // We're about to modify the context CTM to draw the PDF page where we want it, so save the graphics state in case we want to do more drawing
    CGContextSaveGState(context);
    // CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any
    // base rotations necessary to display the PDF page correctly.
    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true);
    // And apply the transform.
    CGContextConcatCTM(context, pdfTransform);
    // Finally, we draw the page and restore the graphics state for further manipulations!
    CGContextDrawPDFPage(context, page);
    CGContextRestoreGState(context);

}

当我删除drawRect方法实现时,内存分配问题被忽略但很明显它无法打印pdf。

2 个答案:

答案 0 :(得分:0)

尝试将@autoreleasepool放入循环中:

do
{
    @autoreleasepool
    {
        __pageDictionary = CFDictionaryGetValue(_allPages,
                                            __pageID);
        CUIPageViewController *__pageViewController = [self _pageWithID:__pageID];
        [__pageViewController addMainLayers];
        [[APP_DELEGATE assetManager] temporarilyPasteSnapshotSource:__pageViewController.view];
        UIImage *__snapshotImage = [__pageViewController captureSnapshot];
        [[AOAssetManager sharedManager] saveImage:__snapshotImage
                         forPublicationBundle:_publicationTileViewController.publication.bundle
                                       pageID:(__bridge NSString *)__pageID];
        [[APP_DELEGATE assetManager] removePastedSnapshotSource:__pageViewController.view];
            __snapshotImage = nil;
        __pageViewController = nil;

        ind += 6 * 0.1 / CFDictionaryGetCount(_allPages);
    }
}
while (![(__bridge NSString *)(__pageID = CFDictionaryGetValue(__pageDictionary,

这将在每次循环时刷新自动释放池,释放所有自动释放的对象。

答案 1 :(得分:0)

如果这实际上是ARC的问题,我会感到惊讶。仍然活着的对象仍然具有对该层的强引用。

  1. [AOAssetManager saveImage:...]对图像有什么作用。你确定没有抓住它吗?

  2. 是_pageWithID:做一些保持指向CUIPageViewController的指针吗?