2014-02-26 129 views
0

我有一個代碼塊,旨在爲每個頁面捕獲基於pdf的自定義視圖的快照。爲了實現它,我將在循環中創建視圖控制器,然後迭代。問題是即使視圖控制器發佈自定義視圖沒有發佈,看起來像現場儀器工具。因爲循環迭代很多,所以它打破了內存(42個生命最高可達500MB)和崩潰。 這裏是迭代代碼;ARC不發佈CALayer

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。

+1

您是否針對您的項目運行了靜態分析器?使用Core Foundation對象發現內存問題非常有用。 – Fogmeister

+0

yeap已經做到了,但問題沒有關係。 –

回答

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, 

這將每次刷新自動釋放池循環時,釋放所有的自動釋放對象。

+0

它刪除了CG柵格數據內存分配,但第一個CUIPDFView(CALayer)內存部分從300MB增加到了600MB。結論無法解決它。 –

0

如果這實際上是ARC的問題,我會感到驚訝。一個仍然活着的對象仍然對該圖層有很強的參考。

  1. [AOAssetManager saveImage:...]對圖像有什麼影響。你確定它沒有抓住它嗎?

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

+0

第一個是保存uiimage。 pageWithId用於第一次分配CUIPageViewController,然後返回它。 –