【发布时间】:2012-12-12 08:20:18
【问题描述】:
我正在使用PhotoScrollerNetwork project 为我的项目中的视图提供单个高分辨率图像并自动平铺它,因此可以正确管理内存。它使用此代码块将完整的高分辨率图像绘制到内存中,以便可以从中计算出图块。
-(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);,内存是ok的,所以我认为内存中保留了一些东西,但我无法得到什么......总是调用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 的问题 :-)。最好的办法可能是将您修改后的项目,压缩,然后将其发布到一个投递箱帐户,以便我可以查看它(但可能要到本周末才会发生)。