2012-12-12 27 views
2

我使用PhotoScrollerNetwork project爲我的項目中的視圖提供單個高分辨率圖像,並自動對其進行拼貼,以便正確管理內存。它使用這塊代碼將完整的高分辨率圖像繪製到內存中,以便可以從中計算拼貼塊。CGContextDrawImage內存不釋放

-(void)drawImage:(CGImageRef)image { 
     madvise(ims[0].map.addr, ims[0].map.mappedSize - ims[0].map.emptyTileRowSize, MADV_SEQUENTIAL); 

     unsigned char *addr = ims[0].map.addr + ims[0].map.col0offset + ims[0].map.row0offset * ims[0].map.bytesPerRow; 
     CGContextRef context = CGBitmapContextCreate(addr, ims[0].map.width, ims[0].map.height, bitsPerComponent, ims[0].map.bytesPerRow, colorSpace, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little); 
     assert(context); 
     CGContextSetBlendMode(context, kCGBlendModeCopy); // Apple uses this in QA1708 
     CGRect rect = CGRectMake(0, 0, ims[0].map.width, ims[0].map.height); 
     CGContextDrawImage(context, rect, image); 
     CGContextRelease(context); 

     madvise(ims[0].map.addr, ims[0].map.mappedSize - ims[0].map.emptyTileRowSize, MADV_FREE); 
} 

在類的dealloc方法中,IMS被釋放( '自由(IMS)'),所以這應妥善處理。但是,如果我反複製作新的視圖(並因此調用drawImage),我的記憶力就會越來越豐滿。我發現如果我發表評論CGContextDrawImage(context, rect, image);,內存是好的,所以我認爲內存中保存了一些內容,但是我無法得到什麼......總是調用dealloc方法,所以這不是問題。

編輯: 我的形象也被釋放正確,這是完整的流程:

- (void)myFunc { 
     CFDictionaryRef options = [self createOptions]; 
     CGImageRef image = CGImageSourceCreateImageAtIndex(imageSourcRef, 0, options); 
     CFRelease(options); 
     CFRelease(imageSourcRef); 
     if (image) { 
      [self decodeImage:image]; 
      CGImageRelease(image); 
     } 
} 

- (void)decodeImage:(CGImageRef)image { 
    assert(decoder == cgimageDecoder); 

    size_t width = CGImageGetWidth(image); 
    size_t height = CGImageGetHeight(image); 

#if LEVELS_INIT == 0 
    zoomLevels = [self zoomLevelsForSize:CGSizeMake(width, height)]; 
    ims = calloc(zoomLevels, sizeof(imageMemory)); 
#endif 

    [self mapMemoryForIndex:0 width:width height:height]; 

    [self drawImage:image]; 
    [self createLevelsAndTile]; 
} 
+0

爲什麼你沒有發佈問題到github支持鏈接?這裏幾乎不可能有任何人(但我,這個項目的作者)可以在SO上回答你的問題:-)。可能最好的辦法是把你的修改後的項目,壓縮,並將其發佈到投箱帳戶,這樣我可以看看它(但可能不會發生在這個週末)。 –

回答

0

運行本地從束圖像和網絡圖像,它出現任何顯著泄漏了。這與iOS7和Xcode 5.

+0

我有12 MB的jpg圖像,每件事情都很完美,但CGContextDrawImage(context,rect,image);導致內存高達360 MB,並且應用程序崩潰。這是由於madvise?任何替代此功能 - (void)drawImage:(CGImageRef)圖像? –

+0

好吧,試着評論一下,看看。我不知道 - 這個項目運行在一個內存有限的原始iPad上。 –

+0

我試圖刪除madvise,但它似乎像CGContextDrawImage內部解壓縮整個JPG這就是爲什麼它採取這麼多的內存。我正在使用500 MB RAM的iPad mini上運行。和它的崩潰。 –