Ios 未释放CGContextDrawImage内存
我正在使用为项目中的视图提供单个高分辨率图像,并自动平铺它,以便正确管理内存。它使用这段代码将完整的高分辨率图像绘制到内存中,以便可以从中计算出分幅Ios 未释放CGContextDrawImage内存,ios,memory,cgcontext,Ios,Memory,Cgcontext,我正在使用为项目中的视图提供单个高分辨率图像,并自动平铺它,以便正确管理内存。它使用这段代码将完整的高分辨率图像绘制到内存中,以便可以从中计算出分幅 -(void)drawImage:(CGImageRef)image { madvise(ims[0].map.addr, ims[0].map.mappedSize - ims[0].map.emptyTileRowSize, MADV_SEQUENTIAL); unsigned char *addr = ims[0
-(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被释放(“free(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];
}
运行本地包映像和网络映像时,似乎任何重大泄漏都消失了。这与iOS7和Xcode 5有关。为什么不在github支持链接上发布问题?这里的任何人(除了我,这个项目的作者)都不太可能回答你的问题,所以:-)。也许最好的办法是把你修改过的项目打包,然后把它发到一个投递箱账户,这样我就可以看一看了(但可能要到这个周末才会发生)。我有12MB的jpg图像,除了CGContextDrawImage(上下文、rect、图像)外,一切都很完美;导致内存高达360 MB,应用程序崩溃。这是因为马德维斯吗?这个函数的任何替代方法-(void)drawImage:(CGImageRef)image???好吧,试着把它注释出来看看。我不知道-这个项目运行在一个原始的iPad上,它的内存严重受限。我尝试删除madvise,但似乎CGContextDrawImage内部解压了整个jpg,这就是它占用如此多内存的原因。我使用的是iPad mini,内存为500 MB。和它的崩溃。