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